直接在客户端访问 JSON 对象的属性
由于与 Sharepoint 不兼容,我的任务是将一页 ASP.NET MVC 3 Web 应用程序转换为 ASP.NET 3.5 Web 窗体应用程序。我无法访问 Web 表单应用程序的 json 结果中对象的属性。谁能告诉我我做错了什么?另外,当使用 Web 表单返回 json 数据时,使用 WCF 服务还是常规 Web 服务更好?谁能给我一些例子吗?使用内置的 Javascript 序列化器还是 JSON.net 库更好?这是我的代码 -
MVC 方法 -
public ActionResult LoadPerson()
{
var p = new Person;
p.Name = "Bob";
return Json(new { value = p}, JsonRequestBehavior.AllowGet); //what is the equivalent of this in webforms so I can access the properties directly?
}
MVC javascript 文件 -
var person;
$.ajax({
url: 'Home/LoadPerson',
type: 'GET',
async: false,
contentType: 'application/json;',
dataType: 'json',
success: function (result) {
person = result.value;
}
});
alert(person.Name); //works fine.
Web 表单代码隐藏 -
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string LoadPerson()
{
var p = new Person();
p.Name = "Bob";
var serializer = new JavaScriptSerializer();
return serializer.Serialize(p);
}
Web 表单 javascript -
var person;
$.ajax({
url: 'Default.aspx/LoadPerson',
type: 'POST',
async: false,
contentType: 'application/json;',
dataType: 'json',
success: function (result) {
person = result.d;
}
});
alert(person.Name); //undefined. Why?
I've been tasked with converting a one-page ASP.NET MVC 3 web app to ASP.NET 3.5 Web Forms app due to some incompability with Sharepoint. I'm unable to the access the property of an object in json result of the web forms app. Can anyone tell me what I'm doing wrong? Also, is it better to use a WCF service or a regular web service when using web forms for returning json data? Can anyone give me some examples? Is it better to use in-built Javascript serializer or JSON.net library? Here's my code -
MVC method -
public ActionResult LoadPerson()
{
var p = new Person;
p.Name = "Bob";
return Json(new { value = p}, JsonRequestBehavior.AllowGet); //what is the equivalent of this in webforms so I can access the properties directly?
}
MVC javascript file -
var person;
$.ajax({
url: 'Home/LoadPerson',
type: 'GET',
async: false,
contentType: 'application/json;',
dataType: 'json',
success: function (result) {
person = result.value;
}
});
alert(person.Name); //works fine.
Web Forms code-behind -
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string LoadPerson()
{
var p = new Person();
p.Name = "Bob";
var serializer = new JavaScriptSerializer();
return serializer.Serialize(p);
}
Web Forms javascript -
var person;
$.ajax({
url: 'Default.aspx/LoadPerson',
type: 'POST',
async: false,
contentType: 'application/json;',
dataType: 'json',
success: function (result) {
person = result.d;
}
});
alert(person.Name); //undefined. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是由于手动 JSON 序列化页面方法的响应造成的。对服务的响应运行
JSON.parse()
后,jQuery 会留下一个 JSON 字符串,而不是具有.Name
等属性的对象。ASP.NET 自动处理该步骤(使用 JavaScriptSerializer内部本身,不少)。如果您只是返回对象并让 ASP.NET 处理翻译,它应该按您的预期工作:
请参阅这篇文章以获取更详细的说明:http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/
当您进行更改时代码,我建议避免使用
async: false
方法。由于 JavaScript 在单个共享线程中运行,并且在大多数浏览器中与 UI 渲染共享该线程,因此任何同步代码都会引入一系列性能问题。这甚至可能导致您的脚本显示无响应的脚本对话框,让您的用户可以选择取消其执行。Your problem is due to manually JSON serializing the response from your page method. After running
JSON.parse()
on the response from your service, jQuery is left with a JSON string instead of an object with properties like.Name
.ASP.NET automatically handles that step (using JavaScriptSerializer internally itself, no less). If you just return the object and let ASP.NET handle the translation, it should work as you expect:
See this post for a more detailed explanation: http://encosia.com/asp-net-web-services-mistake-manual-json-serialization/
While you're changing the code, I recommend avoiding the
async: false
approach. Since JavaScript runs in single, shared thread, and shares that thread with UI rendering in most browsers, any synchronous code introduces a whole host of performance issues. That may even lead to your script presenting an unresponsive script dialog, giving your users the option to cancel its execution.