[英]ASP.NET WebMethod returning object as json but not in response method
我有一個簡單的網絡方法:
<WebMethod(Description:="Does something.")> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Shared Function ReturnJSONData() As Person
Dim guy As New Person
guy.Name = "Joe"
guy.Age = 8
Return guy
End Function
這是我調用ajax方法的地方:
function GetPerson() {
PageMethods.ReturnJSONData(OnWSRequestComplete1);
}
function OnWSRequestComplete1(result) {
alert(result.d);
}
當我使用Firebug之類的工具時,可以看到JSON結果:
{"d":{"__type":"Person","Name":"Joe","Age":8}}
但是,當我調用“ alert(result.d)”時,我的狀態不確定。 我想念什么嗎?
從WebMethod獲得響應時,您需要評估響應,因為它以字符串形式返回。 我會猶豫不要在您的代碼中使用eval
,因為存在安全風險。
如果您使用的是jQuery,則可以調用jQuery.parseJSON(result)
,它將返回您期望的實際Javascript對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.