通过POST调用支持AJAX的Web服务,但是使用GET它总是返回xml

时间:2014-01-20 01:11:55

标签: jquery asp.net asmx asp.net-4.0

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

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

  • 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;
        }
    }
    

1 个答案:

答案 0 :(得分:3)

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

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

  

两个简单的要求

     

正如我前面提到的,一个规定就是这些   ScriptServices仅返回JSON序列化结果(如果是)   要求得当。否则,即使是标有的服务   属性将返回XML而不是JSON。我只能假设那个   误解ASMX服务不能的部分原因   用JSON回复。

     

斯科特格思里有一篇关于具体要求的好文章   从ScriptServices中强制JSON。总结一下,请求   服务方法必须满足两个要求:

     

(1)Content-Type - HTTP请求必须声明内容类型   应用/ JSON 即可。这会通知ScriptService它会   接收其参数为JSON,它应该以实物形式响应。

     

(2)HTTP方法 - 默认情况下,HTTP请求必须是POST   请求即可。可以规避这一要求,但确实如此   建议在处理JSON时坚持使用HTTP POST请求。

     

就是这样。

     

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

相关问题