asp.net-mvc-3 – ASP.NET MVC 3 JSONP:这适用于JsonValueProviderFac
|
Phil Haack在如何使用 JSON,数据绑定和数据验证方面拥有出色的 blog post. 输入浏览器的“同源策略安全限制”.和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字符串并将其绑定到模型而言,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(")");
}
}
}
然后在你的控制器动作中: public ActionResult Foo()
{
return new JsonpResult
{
Data = new { Prop1 = "value1",Prop2 = "value2" },JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
}
可以使用 $.getJSON('http://domain.com/home/foo?jsoncallback=?',function(data) {
alert(data.Prop1);
}); (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 你可以从请求变量确定时区吗?
- asp.net-mvc – ASP.NET MVC网格控件比较
- asp.net-mvc – 在发送到视图之前如何修改控制器动作中的表
- asp.net-mvc-3 – MVC 3 knockoutjs:在使用EditorFor作为布
- asp.net-web-api – Web API中的多态:单端点可能?
- asp.net-mvc – UpdateModel前缀 – ASP.NET MVC
- 在ASP.NET MVC3中实现的示例项目插件jquery文件上传插件
- 为什么ASP.NET页面在本地主机上比在生产服务器上慢得多
- asp.net – IIS自定义错误未显示自定义错误页面
- ASP.net HTTP 404 – 找不到文件而不是MaxRequestLength异常
- 如何强制我的ASP.net 2.0应用程序重新编译
- asp.net-core – 从与xproj相同的解决方案引用cs
- asp.net-mvc – ASP.NET MVC的最佳实践
- asp.net-mvc-4 – ASP.NET MVC 4移动功能
- asp.net-mvc-2 – Plus()在MVC中的参数引起404在
- 单元测试 – 如何在ASP MVC 5(Microsoft.AspNet.
- asp.net-mvc-3 – 如何在MVC3上使用authorize属性
- asp.net-mvc-3 – 用于基本身份验证的asp mvc 3
- asp.net – 如何将服务器时间转换为本地时间
- asp.net-mvc-3 – MVC 3不显眼的验证 – 有条件地
