Phil Haack 有一个出色的 blog post了解如何使用 JSON、数据绑定(bind)和数据验证。
输入浏览器的“同源策略安全限制”。和 JSONP,您使用 $.getJSON() 检索内容。
是否有内置的 MVC 3 方法可以做到这一点,或者我是否需要遵循 posts like this 的建议?可以发内容吗?我问这个问题是因为我的同事实现了 JsonPfilterAttribute 以及其他东西来使这项工作正常进行。如果 MVC 3 中已经存在某些内容,显然最好避免这种情况。
编辑:
摘要:除了访问 POST
变量之外,一切正常,即如何在上下文中访问 POST
变量? (在最后一段代码中注释标记)
我选择使用这种格式来调用服务器:
$.ajax({
type: "GET",
url: "GetMyDataJSONP",
data: {},
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
jsonpCallback: "randomFunctionName"
});
这会产生以下响应:
randomFunctionName([{"firstField":"111","secondField":"222"}]);
如果我使用 GET
,所有这些都可以很好地工作。但是,我仍然无法让它作为 POST
工作。这是 Nathan Bridgewater here 发布的原始代码。此行未找到 POST 数据:
context.HttpContext.Request["callback"];
要么我应该以某种方式访问 Form
,要么 MVC 数据验证器正在删除 POST 变量。
应该如何编写 context.HttpContext.Request["callback"];
来访问 POST
变量,或者 MVC 是否出于某种原因删除这些值?
namespace System.Web.Mvc
{ public class JsonpResult : ActionResult
{ public JsonpResult() {}
public Encoding ContentEncoding { get; set; }
public string ContentType { get; set; }
public object Data { get; set; }
public string JsonCallback { get; set; }
public override void ExecuteResult(ControllerContext context)
{ if (context == null)
throw new ArgumentNullException("context");
this.JsonCallback = context.HttpContext.Request["jsoncallback"];
// This is the line I need to alter to find the form variable:
if (string.IsNullOrEmpty(this.JsonCallback))
this.JsonCallback = context.HttpContext.Request["callback"];
if (string.IsNullOrEmpty(this.JsonCallback))
throw new ArgumentNullException(
"JsonCallback required for JSONP response.");
HttpResponseBase response = context.HttpContext.Response;
if (!String.IsNullOrEmpty(ContentType))
response.ContentType = ContentType;
else
response.ContentType = "application/json; charset=utf-8";
if (ContentEncoding != null)
response.ContentEncoding = ContentEncoding;
if (Data != null)
{ JavaScriptSerializer serializer = new JavaScriptSerializer();
response.Write(string.Format("{0}({1});", this.JsonCallback,
serializer.Serialize(Data)));
} } }
//extension methods for the controller to allow jsonp.
public static class ContollerExtensions
{
public static JsonpResult Jsonp(this Controller controller,
object data)
{
JsonpResult result = new JsonpResult();
result.Data = data;
result.ExecuteResult(controller.ControllerContext);
return result;
}
}
}
最佳答案
就接收 JSON 字符串并将其绑定(bind)到模型而言,JsonValueProviderFactory
在 ASP.NET MVC 3 中开箱即用地完成这项工作。但是没有内置任何内容用于输出JSONP。您可以编写自定义 JsonpResult
:
public class JsonpResult : JsonResult
{
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
var request = context.HttpContext.Request;
var response = context.HttpContext.Response;
string jsoncallback = (context.RouteData.Values["jsoncallback"] as string) ?? request["jsoncallback"];
if (!string.IsNullOrEmpty(jsoncallback))
{
if (string.IsNullOrEmpty(base.ContentType))
{
base.ContentType = "application/x-javascript";
}
response.Write(string.Format("{0}(", jsoncallback));
}
base.ExecuteResult(context);
if (!string.IsNullOrEmpty(jsoncallback))
{
response.Write(")");
}
}
}
然后在你的 Controller 操作中:
public ActionResult Foo()
{
return new JsonpResult
{
Data = new { Prop1 = "value1", Prop2 = "value2" },
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
可以通过 $.getJSON()
从另一个域使用它:
$.getJSON('http://example.com/home/foo?jsoncallback=?', function(data) {
alert(data.Prop1);
});
关于asp.net-mvc-3 - ASP.NET MVC 3 JSONP : Does this work with JsonValueProviderFactory?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4795201/