gpt4 book ai didi

jquery - 通过 POST 调用支持 AJAX 的 Web 服务可以工作,但使用 GET 时它总是返回 xml

转载 作者:行者123 更新时间:2023-12-01 07:59:11 25 4
gpt4 key购买 nike

我在 ASP.NET 4.0 网站中以两种不同的方式调用 Web 服务(同一网站中的 asmx 服务)方法。当 asmx Web 服务方法用 [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)] 修饰时,第一个方法成功并始终返回有效的 JSON 对象。 .

但是第二种方法失败了,因为返回的数据是XML而不是JSON,即使我已经装饰了asmx方法 [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] (我无法理解为什么使用 GET 时没有返回 JSON,但使用 POST 时却返回 JSON?)

  • POST服务电话

    var serviceurl = "http://localhost:49441/WebService1.asmx/LoginUser" ;
    $.ajax({
    url: serviceurl,
    type: 'POST',
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify({ userName: userName, password: password }),
    dataType: "json",
    success: function (msg) {
    alert('Web service call succeeded. ' + msg.d);
    },
    error: function (error) { alert('ERROR has occurred!'); alert(JSON.stringify(error)) }
    });
  • GET服务电话

    var serviceurl = "http://localhost:49441/WebService1.asmx/LoginUser" ;
    $.ajax({
    url: serviceurl,
    type: 'GET',
    contentType: "application/json; charset=utf-8",
    data: 'userName='+ userName + '&password=' + password,
    dataType: "json",
    success: function (msg) {
    alert('Web service call succeeded. ' + msg.d);
    },
    error: function (error) { alert('ERROR has occurred!'); alert(JSON.stringify(error)) }
    });
<小时/>
  • 编辑1:

    Web服务代码如下。使用时POST我只需更改代码以使用 UseHttpGet = false对于被调用的方法。

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 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 WebService1 : System.Web.Services.WebService
    {
    [WebMethod]
    [PrincipalPermission(SecurityAction.Assert, Unrestricted = true)]
    [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
    public bool LoginUser(string userName, string password)
    {
    bool authenticated = false;

    if (userName.ToLower() == "mike" && password.ToLower() == "abcd")
    {

    authenticated = true;
    }
    return authenticated;
    }
    }

最佳答案

根据我在Dave Ward的博客中的以下URL中读到的内容,看来有必要使用POSTExplanation on why POST is necessary if we are to receive JSON and not Xml when using jQuery ,否则启用 ASP.Net AJAX 的 Web 服务可能会使用 XML 进行响应,即使其被修饰为返回 JSON。我已粘贴上述 URL 中与我的问题相关的部分。

(所以我从这一切中学到的教训是,在从 jQuery 调用支持 AJAX 的 Web 服务(即 asmx 服务)时使用 POST。)

Two simple requirements

As I alluded to earlier, the one stipulation is that these ScriptServices only return JSON serialized results if they are requested properly. Otherwise, even a service marked with the attribute will return XML instead of JSON. I can only assume that’s part of the reason for the misconception that ASMX services cannot respond with JSON.

Scott Guthrie has a great post on the specific requirements for coercing JSON out of ScriptServices. To summarize that, requests to the service methods must meet two requirements:

(1) Content-Type – The HTTP request must declare a content-type of application/json. This informs the ScriptService that it will receive its parameters as JSON and that it should respond in kind.

(2) HTTP Method – By default, the HTTP request must be a POST request. It is possible to circumvent this requirement, but it is advisable to stick with HTTP POST requests when dealing with JSON.

That’s it.

As long as those two requirements are satisfied, anything from low-level XMLHttpRequest code, to third-party libraries like jQuery, to ASP.NET AJAX itself can easily retrieve JSON serialized data from ASMX services.

关于jquery - 通过 POST 调用支持 AJAX 的 Web 服务可以工作,但使用 GET 时它总是返回 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21224781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com