Todd Krueger
Todd Krueger

Reputation: 137

How To Use Object Returned By VB.NET Web Service

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?

Upvotes: 0

Views: 1443

Answers (2)

Avitus
Avitus

Reputation: 15958

if you do:

$(e).text()

You'll get your text;

So change this to be:

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).text());
},
error: function (e) {
    alert("There was an error retrieving records: " + e);
}
});
}//END jQuerySerial

Upvotes: 0

SLaks
SLaks

Reputation: 887413

ASP.Net WebMethods wrap their return values in a JSON object with a d property. (You can see this in the response body in the browser's developer tools)

You need to write e.d to get the actual value.

Upvotes: 1

Related Questions