How can we call web methods (web service) using JSON.
This article will explain how to call webmethods using JSON. Since if we call web methods in server side then we can’t avoid post back. So whenever we require asynchronous call, we can use xmlhttprequest and implementation is simple. Once we get the response, we can manipulate according to our requirement.
This article will explain how to call webmethods using JSON.
Step 1: Create xmlhttp object based on the browser.
var objxmlhttp;
if(is_ie)
{ // for IE
var objname = (is_ie5)?Microsoft.xmlhttp:msxml2.xmlhttp;
objxmlttp = new activexobject (objname );
objxmlttp.onreadystatechange = onreadystatechange
}
else
{
//for other browsers
objxmlhttp = new xmlhttprequest();
objxmlhttp.onload = onreadystatechange;
}
Step 2: Create handler for the object
function onreadystatechange()
{
if(objxmlhttp.readystate == 4 or objxmlhttp.readystate='complete')
{
message = objxmlHttp.responseText;
//do something
}
}
Step 3: Define the url where you have called webmethods, even it can be aspx page and call the request. The XMLHTTP request is asynchronous and uses onreadystatechange event handler to process the data returned by the call.
objxmlhttp.open('Get',url,true); // true-async mode;
objxmlhttp.send(null);