I have a Javascript function that passes a string into a VB.NET Web Method. Here is the Javascript
function jQuerySerial() {
//I SET A VARIABLE TO THE STRING I IS PASSED INTO MY WEB METHOD
var str = "{ 'str': 'hello world'}";
//THEN I PASS IT INTO MY VB.NET WEB METHOD
$.ajax({
type: "POST",
url: "test_WebService.asmx/testWebService",
data: str,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (e) {
alert("It worked: " + e);
},
error: function (e) {
alert("There was an error retrieving records: " + e);
}
});
}//END jQuerySerial
Here is the VB.NET web Method. The Web Method simply takes the string and sends it back to the Javascript:
<WebMethod( )> _
Public Function testWebService(str As String) As String
Return str
End Function
When the Web Method returns successfully my AJAX block returns the following message:
"It worked: [object Object]"
So my question is, how do I use the returned object? The object should be a string containing the words “hello world” but how do I access the string value?
ASP.Net WebMethods wrap their return values in a JSON object with a
dproperty. (You can see this in the response body in the browser’s developer tools)You need to write
e.dto get the actual value.