如何通过JavaScript发送跨域POST请求?
注意 - 它不应该刷新页面,然后我需要抓取并解析响应.
我希望跨域返回一些JSON,我知道这样做的方法是通过JSONP而不是纯JSON.我正在使用ASP.net MVC所以我正在考虑只是扩展JSONResult类型然后extendig Controller,以便它还实现了一个Jsonp方法.这是最好的解决方法,还是内置的ActionResult可能会更好?
编辑:我继续前进并做到了.仅供参考,我添加了一个新结果:
public class JsonpResult : System.Web.Mvc.JsonResult
{
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
HttpResponseBase response = context.HttpContext.Response;
if (!String.IsNullOrEmpty(ContentType))
{
response.ContentType = ContentType;
}
else
{
response.ContentType = "application/javascript";
}
if (ContentEncoding != null)
{
response.ContentEncoding = ContentEncoding;
}
if (Data != null)
{
// The JavaScriptSerializer type was marked as obsolete prior to .NET Framework 3.5 SP1
#pragma warning disable 0618
HttpRequestBase request = context.HttpContext.Request;
JavaScriptSerializer serializer …Run Code Online (Sandbox Code Playgroud)