请求Ashx页面,返回的json数据,使用Sufei的HtmlHelper请求后无数据返回,请问怎么处理。使用HttpRequest, WebClient, JSONP都不行,是否可以用webbrowser来实现啊?
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 }
你搜一下,跨域访问的三种方法。返回jsonp格式
跨域名,但是不能修改服务端返回的格式,只能返回JSON数据,用JSONP报异常。
http://home.cw.uitv.com.cn/Weather.ashx?service=1&cityid=101010100 ,请求地址是这个,你可以试试,如何调用?
http://doc.shenjianshou.cn/developmentSkills/useAJAX.html