【发布时间】:2012-02-04 02:28:10
【问题描述】:
由于与 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?
【问题讨论】:
标签: jquery asp.net-mvc webforms