I am trying to send and return a simple string to a VB.NET web method from Javascript using AJAX. Here is the Javascript/jQuery script I am using:
function jQuerySerial() {
//I SET A VARIABLE TO THE STRING I WANT TO PASS INTO MY WEB METHOD
var str = "Hello World";
//AND TRY TO 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
And here is the very simple VB.net Web Method. The Web Method does nothing but get the string and then pass it back to Javascript:
<WebMethod( )> _
Public Function testWebService(str As String) As String
Return str
End Function
When I attempt to run this the error: function fires and returns a message saying:
"There was an error retrieving records: [object Object]"
I have many, many other Web Methods in this same Web Service class that manipulate database records and they all work. But, this is the first one I have ever tried to write using the $.ajax syntax and return something to the calling Javascript so I am completely clueless on whats wrong here.
Any suggestions on how to make this work would be appreciated. Thanks
It looks like the issue here is that you’re passing a simple string to the Web Service when it is expecting a JSON object. See this article on common issues with jQuery and ASP.NET web services (specifically item 2):
http://encosia.com/3-mistakes-to-avoid-when-using-jquery-with-aspnet-ajax/