Jquery ajax return value

I have an asp.net application with a static page method. I use the codes below to call a method and get its return value.

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

What I got is [object Object].

Below is my static method. And I also have EnablePageMethods="true" EnablePartialRendering="true" in my ScriptManager.

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

Is there any way to get the return value?

+4
source share
4 answers

Try using the Chrome developer tools or the firebug plugin from Firfox. Not sure if IE developer tools allow checking ajax calls?

The resulting string you are looking for is really in the result object. You need to look at the variable d. I remember reading somewhere why it was, I think it looks ASP.NET: |

Try:

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

WITH#

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

Hope this helps.

+5
source

It’s just that you are stuck in .d, which is presented in ASP.NET 3.5's JSON response. To quote Dave Ward,

If you are familiar with ".d", I mean this is just a security feature added by Microsoft in ASP.NET 3.5s version of ASP.NET AJAX. Encapsulating the JSON response in the parent object provides protection against the particularly unpleasant XSS vulnerability .

So, just check if .d , and then unzip it. Change your success function as follows.

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

How about this?

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

I have found a solution.

You can use parseJSON to get the result http://api.jquery.com/jQuery.parseJSON/

or change the data type to html to see the actual value. http://docs.jquery.com/Specifying_the_Data_Type_for_AJAX_Requests

Thanks guys for your help.

0
source

All Articles