首页 新闻 搜索 专区 学院

跨域抓取Ashx页面,返回的json数据,请问有什么办法,谢谢!

0
悬赏园豆:20 [已解决问题] 解决于 2016-01-22 15:56

请求Ashx页面,返回的json数据,使用Sufei的HtmlHelper请求后无数据返回,请问怎么处理。使用HttpRequest, WebClient, JSONP都不行,是否可以用webbrowser来实现啊?

我的理想之路的主页 我的理想之路 | 初学一级 | 园豆:143
提问于:2015-07-11 08:58
< >
分享
最佳答案
0
 1 //跨域登陆
 2 function loginSubmit(callback) {
 3         var loginUrl = '/Login_Submit';
 4         $.ajax({
 5             url: loginUrl,
 6             type: "GET",
 7             dataType: "jsonp",
 8             jsonpCallback: "localJsonpCallback",
 9             data: $("#formlogin").serialize(),
10             error: function (XMLHttpRequest, textStatus, errorThrown) {
11                 
12             },
13             beforeSend: function () {
14             },
15             success: function (result) {
16                 if (result) {
17                    
18                 }
19             }
20         });
21     
22 };
23 
24 
25 function localJsonpCallback(result) {
26     if (result) {
27         if (result.success) {
28             window.location = result.data;
29             return;
30         } else {
31             alert(result.message);
32         }
33     }
34 }



public static class ContollerExtensions
    {
        /// <summary>
        /// Extension methods for the controller to allow jsonp.
        /// </summary>
        /// <param name="controller">The controller.</param>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public static JsonpResult Jsonp(this Controller controller, object data)
        {
            JsonpResult result = new JsonpResult()
            {
                Data = data,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };

            return result;
        }
    }
    public class JsonpResult : JsonResult
    {
        private static readonly string JsonpCallbackName = "callback";
        private static readonly string CallbackApplicationType = "application/json";

        /// <summary>
        /// Enables processing of the result of an action method by a custom type that inherits from the <see cref="T:System.Web.Mvc.ActionResult"/> class.
        /// </summary>
        /// <param name="context">The context within which the result is executed.</param>
        /// <exception cref="T:System.ArgumentNullException">The <paramref name="context"/> parameter is null.</exception>
        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            if ((JsonRequestBehavior == JsonRequestBehavior.DenyGet) &&
                  String.Equals(context.HttpContext.Request.HttpMethod, "GET"))
            {
                throw new InvalidOperationException();
            }
            var response = context.HttpContext.Response;
            if (!String.IsNullOrEmpty(ContentType))
                response.ContentType = ContentType;
            else
                response.ContentType = CallbackApplicationType;
            if (ContentEncoding != null)
                response.ContentEncoding = this.ContentEncoding;
            if (Data != null)
            {
                String buffer;
                var request = context.HttpContext.Request;
                var serializer = new JavaScriptSerializer();
                if (request[JsonpCallbackName] != null)
                    buffer = String.Format("{0}({1})", request[JsonpCallbackName], serializer.Serialize(Data));
                else
                    buffer = serializer.Serialize(Data);
                response.Write(buffer);
            }
        }
    }

 

1 /// <summary>
2         /// 提交登录
3         /// </summary>
4         public JsonpResult Login_Submit()
5         {
6             var result = new ResponseResult();  
7             Response.AddHeader("p3p", "CP=\"IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT\"");
8             return ContollerExtensions.Jsonp(this, result);
9         }

 

收获园豆:19
晓道 | 菜鸟二级 |园豆:297 | 2015-07-11 22:02
其他回答(2)
0

你搜一下,跨域访问的三种方法。返回jsonp格式

收获园豆:1
LiloT | 园豆:6 (初学一级) | 2015-07-11 09:19

跨域名,但是不能修改服务端返回的格式,只能返回JSON数据,用JSONP报异常。

支持(0) 反对(0) 我的理想之路 | 园豆:143 (初学一级) | 2015-07-11 09:54

http://home.cw.uitv.com.cn/Weather.ashx?service=1&cityid=101010100 ,请求地址是这个,你可以试试,如何调用?

支持(0) 反对(0) 我的理想之路 | 园豆:143 (初学一级) | 2015-07-11 09:55
0

http://doc.shenjianshou.cn/developmentSkills/useAJAX.html

wangm_xjtu | 园豆:224 (菜鸟二级) | 2016-05-25 15:05
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册