I am trying to get an ajax get to a webmethod in code behind. The problem is I keep getting the error "parserror" from the jQuery onfail
method.
If I change the GET to a POST everything works fine. Please see my code below.
Ajax Call
<script type="text/javascript">
var id = "li1234";
function AjaxGet() {
$.ajax({
type: "GET",
url: "webmethods.aspx/AjaxGet",
data: "{ 'id' : '" + id + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false,
success: function(msg) {
alert("success");
},
error: function(msg, text) {
alert(text);
}
});
}
</script>
Code Behind
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true,
ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
public static string AjaxGet(string id)
{
return id;
}
Web.config
<webServices>
<protocols>
<add name="HttpGet"/>
</protocols>
</webServices>
The URL being used
......../webmethods.aspx/AjaxGet?{%20%27id%27%20:%20%27li1234%27}
As part of the response it is returning the html for the page webmethods.
Any help will be greatly appreciated.
You could also check out http://www.json.org/js.html JSON.stringify where it accepts a json object as the parameter and returns a string.
Before all I could say, that you choose not the easiest way. ScriptMethods is easy to use together with ASP.NET ScriptManager and not with jQuery. I’ll recommend you better use JSON-enabled WCF HTTP Services (better as RESTfull Service) instead of ASMX Webservice which you try to use now. Nevertheless, one can makes you code working without using any Microsoft technologies on the client side.
First of all verify Server side.
Verify that you placed Inside of \ and a httpHandlers for asmx extension (ScriptHandlerFactory) also exist in the config:
Verify that [ScriptService] attribute ([System.Web.Script.Services.ScriptService] if you like full names) set for your class inherited from System.Web.Services.WebService.
Now you could test the service. Open in you Web-Browser URL like http://localhost/webmethods.asmx/AjaxGet?id=li1234 If you receive back something like
<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://tempuri.org/">li1234</string>
You can be sure that you service part works fine.
Remark: Independ on “ResponseFormat = System.Web.Script.Services.ResponseFormat.Json” attribute the service answer with XML responses if “Content-Type:application/json;” not set in the request.
Now we’ll fix the client code. I hope that comments which I placed in the following code explain all.
One more small remark. In the last part of code I call one more “complex” web method:
Where
Now only JavaScript code which use in some places JSON plugin, which could be replaced with Crockford's json2.js, if somebody prefer it.
I came here looking for the answer... For others, here's the answer.
(From https://stackoverflow.com/a/2397521)
For those using VB, decorate your method like this:
Data is an object, not a string that looks like an object.
If you use a string, it must be a correctly formatted URL query string, like this: