开发者

How to call C# Rest Web service from javascript

开发者 https://www.devze.com 2022-12-19 02:25 出处:网络
I have created a rest architecture based web service in C# which will return a json string.I w开发者_开发问答ant to access this web service from the javascript How i can call this webservice from java

I have created a rest architecture based web service in C# which will return a json string.I w开发者_开发问答ant to access this web service from the javascript How i can call this webservice from javascript.


Use the XMLHttpRequest object (or it's COM-based brother):

function getJSON()
{
    var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
    xhr.open("GET", "http://mysite.com/myscript.asp", true);
    xhr.onreadystatechange = function ()
    {
        if (xhr.status == 200 && xhr.readystate == 4)
        {
            if (JSON) // provided by json2.js or browsers with native JSON
                var result = JSON.parse(xhr.responseText);
            else
                var result = eval ('(' + xhr.responseText + ')');

            // Do something with the result here
        }
    }
    xhr.send();
}

json2.js is a script for safer JSON parsing, and is available from http://www.json.org/js.html

0

精彩评论

暂无评论...
验证码 换一张
取 消