Jquery 通过POST调用支持AJAX的web服务是可行的,但GET总是返回xml

Jquery 通过POST调用支持AJAX的web服务是可行的,但GET总是返回xml,jquery,asp.net,asmx,asp.net-4.0,Jquery,Asp.net,Asmx,Asp.net 4.0,在ASP.NET4.0网站中,我以两种不同的方式调用web服务(同一网站中的asmx服务)方法。当asmx web服务方法用[ScriptMethod(UseHttpGet=false,ResponseFormat=ResponseFormat.JSON)]修饰时,第一个方法成功并始终返回有效的JSON对象 但是第二个方法失败了,因为返回的数据是XML而不是JSON,尽管我已经用[ScriptMethod(UseHttpGet=true,ResponseFormat=ResponseFormat

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

但是第二个方法失败了,因为返回的数据是XML而不是JSON,尽管我已经用
[ScriptMethod(UseHttpGet=true,ResponseFormat=ResponseFormat.JSON)]修饰了
asmx
方法(我无法理解为什么在使用
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)) }
    });
    
    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)) }
    });
    
  • GET
    service call

    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)) }
    });
    
    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中所读到的内容,似乎有必要使用POST,否则ASP.Net AJAX支持的web服务可能会使用XML响应,即使它被修饰为返回JSON。我已粘贴了上述URL中与我的问题相关的部分

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

两个简单的要求

正如我前面提到的,唯一的规定是 ScriptServices只返回JSON序列化的结果,如果它们是 正确请求。否则,即使标记为 属性将返回XML而不是JSON 部分原因是人们错误地认为ASMX服务不能 用JSON响应

Scott Guthrie有一篇关于 将JSON强制移出ScriptServices 维修方法必须满足两个要求:

(1) 内容类型–HTTP请求必须声明 application/json。这会通知ScriptService它将 以JSON形式接收其参数,并且它应该以实物形式响应。

(2) HTTP方法–默认情况下,HTTP请求必须是POST 请求。可以绕过此要求,但事实并非如此 建议在处理JSON时使用HTTP POST请求

就这样

只要满足这两个要求,从低级XMLHttpRequest代码到第三方库(如jQuery),都可以, 到ASP.NET,AJAX本身可以轻松地从中检索JSON序列化数据 ASMX服务


JS代码在这里没有区别。为了理解为什么在发布时返回XML而不是JSON,我们需要查看a)web服务代码或b)web服务文档。Ok。让我添加asmx web服务代码。可能是启用ajax的web服务只能通过POST调用,而不能通过jQuery获取,但不确定。@Sunil jQuery不关心您使用的HTTP方法。这不是客户端问题。好的。当我在ScriptManager中使用标准JavaScript方法调用相同的服务方法时,它工作正常,在Fiddler中检查时使用GET-Back-the-Second-幕后操作。这是正确的(很高兴你找到了我的帖子)。在本质上是资源获取请求的情况下使用POST总是感觉有点“错误”,但框架这样做是为了在默认情况下确保更大的跨站点安全性。要针对类似的API使用GETs(假设您不需要SOAP),Web API是更新应用程序中的一个不错的选择。它与自动序列化和其他功能非常相似,但支持GET,更灵活的URL编码输入参数,通常是未来的标准方法。Dave-你的博客非常有用/精彩,包含其他地方很难找到的信息。谢谢