简体   繁体   中英

ASP.Net web service always return XML not JSON

Currenly i use asp.net web service but when i call web service method by ajax call it always return XML not json

i try

ASP.Net web service won't return JSON - Always XML

but its also not work for me..

JS :-

$.ajax({
            type: "Post",
            contentType: "application/json; charset=utf-8",
            url: "http://www.quietincomes.com/LoginWebservice.asmx/Demo",
            dataType: "jsonp",
            success: function (data) {
                alert("1" + data);
            },
            error: function (result) {
                alert("2" + JSON.stringify(result));
            }
});

LoginWebservice.asmx :-

 [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string Demo()
    {
        return "Harshit";
    }

where i am wrong..

jsfiddle Example:-

http://jsfiddle.net/EXvqc/

First you have to use Post method to send a request to your web service. And as you have used JSONP as it always looks for the callbacks, and you have to define callback methods for it.

Please Refer

And the other thing you have to add like following

[System.Web.Script.Services.ScriptService]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class LoginWebservice : System.Web.Services.WebService
    {

        [WebMethod]
        public string Demo()
        {
            return "Harshit";
        }
    }

Indicates that a Web service can be invoked from script. This class cannot be inherited.

Your aspx/HTML will contain

$.ajax({
            type: "Post",
            contentType: "application/json; charset=utf-8",
            url: "http://www.quietincomes.com/LoginWebservice.asmx/Demo",
            dataType: "json",
            success: function (data) {
                alert("1" + data);
            },
            error: function (result) {
                alert("2" + JSON.stringify(result));
            }
});

See output below

在此处输入图片说明

JSONP is not JSON, JSONP is used to get over the same origin policy (site A cannot make ajax request to site B). To solve this problem site A will create a script tag:

document.createElement("script")

Then set it's source to site B, usually specifying a callback like www.B?callback=callMe

A typical response of site B would be:

callMe({siteBSays:"hello"});

JQuery hides creating the javascript element for you so it looks like a normal ajax request. Make sure site B has the right response type headers I think it's text/javascript

Another way to do cross domain requests is that site B has a response header that allows site A making ajax requests to it (cors) by setting a response header Access-Control-Allow-Origin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM