jquery ajax调用返回值

我有一个带有静态页面方法的asp.net应用程序。 我正在使用以下代码调用该方法并获取其返回值。

$.ajax({ type: "POST", url: "myPage/myMethod", data: "{'parameter':'paramValue'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function(result) {alert(result);} }); 

我得到的是[object Object]。

下面是我的静态方法。 我的ScriptManager中也有EnablePageMethods="true" EnablePartialRendering="true"

  [WebMethod] [ScriptMethod] public static string myMethod(string parameter) { return "Result"; } 

有没有办法让我获得返回值?

尝试使用Chrome开发者工具或Firfox的firebug插件。 不确定IE的开发者工具是否允许您检查ajax调用?

您要查找的结果字符串实际上位于结果对象中。 你需要查看d变量。 我记得在某处读到这是为什么,我认为这是ASP.NET游戏:|

尝试:

 success: function(data) {alert(data.d);} 

C#

 [WebMethod] public static string GetTest(string var1) { return "Result"; } 

希望这可以帮助。

只是你被困在ASP.NET 3.5的JSON响应中引入的.d。 引用戴夫沃德,

如果您不熟悉我所指的“.d”,它只是Microsoft在ASP.NET 3.5版本的ASP.NET AJAX中添加的一个安全function。 通过将JSON响应封装在父对象中,该框架有助于防止特别讨厌的XSS漏洞 。

所以只需检查.d是否存在然后解开它。 像这样改变你的成功function。

 success: function(result) { var msg = result.hasOwnProperty("d") ? result.d : result; alert(msg ); } 

那这个呢?

 $.ajax({ type: "POST", url: "myPage/myMethod?paramater=parameter", success: function(result) { alert(result); } }); 

我找到了解决方案。

您可以使用parseJSON获取结果http://api.jquery.com/jQuery.parseJSON/

或将数据类型更改为html以查看实际值。 http://docs.jquery.com/Specifying_the_Data_Type_for_AJAX_Requests

谢谢你们的帮助。