由POST调用AJAX的web服务工作但得到它始终返回XML [英] Calling AJAX enabled web service by POST works but with GET it always return xml

查看:136
本文介绍了由POST调用AJAX的web服务工作但得到它始终返回XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET 4.0中的Web站点调用两种不同方式的Web服务(在同一网站ASMX服务)的方法。第一种方法成功,总是返回时,ASMX Web服务方法都装饰有一个有效的JSON对象[ScriptMethod(UseHttpGet =假,ResponseFormat = ResponseFormat.Json)]

I am calling in ASP.NET 4.0 web site a web service (asmx service in same web site) method in 2 different ways. The first method succeeds and always returns a valid JSON object when the asmx web service method is decorated with [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)].

但由于返回的数据是XML而不是JSON,即使我已经由装修 ASMX 法[ScriptMethod(第二种方法失败UseHttpGet = TRUE,ResponseFormat = ResponseFormat.Json)] 我不明白使用时为什么没有被返回的JSON GET ,但它是当使用 POST

But the second method fails because the data returned is XML rather than JSON, even though I have decorated the asmx method by [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] (I cannot understand why JSON is not being returned when using GET but it is when using 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:

    • EDIT 1:

    Web服务code是如下。当使用 POST 我简单地改变code使用 UseHttpGet = FALSE 为被调用的方法。

    The Web Service code is as below. When using POST I simply change code to use UseHttpGet = false for the method being called.

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


    推荐答案

    看来,它需要使用POST 据我在下面的网址中的戴夫·沃德的博客阅读 为什么POST是必要的说明,如果我们接受JSON和不是XML使用jQuery时 ,否则ASP.Net AJAX支持Web服务可能即使其装饰返回JSON和XML响应。我已经从粘贴,涉及到我的问题上面的URL的部分。

    It appears that its necessary to use POST according to what I read at the following URL in Dave Ward's blog Explanation on why POST is necessary if we are to receive JSON and not Xml when using jQuery, else ASP.Net AJAX enabled web service may respond with XML even when its decorated to return JSON. I have pasted the parts from the above URL that relate to my question.

    (所以我从所有这一切的教训,呼吁支持AJAX的Web服务时,即ASMX服务,从jQuery的使用POST)。

    (So the lesson I have learnt from all this, is to use POST when calling AJAX enabled web services i.e. asmx services, from jQuery.)

    两个简单的要求

    正如我前面提到,一个规定是,这些
      ScriptServices只返回JSON序列的结果,如果他们是
      要求正确。否则,即使一个服务标有
      属性将返回XML而不是JSON。我只能假设这是
      为误解的部分原因是ASMX服务不能
      使用JSON回应。

    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.

    斯科特格思里有具体要求一个伟大的职位
      强迫JSON出ScriptServices的。总之是,要请求
      服务方法必须满足两个要求:

    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)内容类型 - HTTP请求必须声明的内容类型的
      应用程序/ JSON
    。该通知ScriptService,它会
      收到其参数,JSON和应该同样的方式回应。

    (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方法 - 默认情况下,HTTP请求必须是POST
      要求
    。这是可能绕过这个要求,但它是
      建议使用JSON打交道时坚持使用HTTP POST请求。

    (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.

    这就是它。

    只要这两个条件的,由低层次的XMLHtt prequest code什么,要像jQuery 的第三方库,
      到ASP.NET AJAX本身可以轻松地检索JSON序列化从数据
      ASMX服务。

    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.

    这篇关于由POST调用AJAX的web服务工作但得到它始终返回XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆