簡體   English   中英

僅當使用Http Get但是web.config設置正確時,ASP.NET WebService錯誤地返回XML而不是JSON

[英]ASP.NET WebService mistakenly returning XML instead of JSON only when using Http Get but web.config is set up right

症狀:當我發出Web服務請求時(從使用.ajax的JQuery到ASP.NET .asmx文件),如果是使用GET而不是POST,則.asmx文件始終返回XML而不是JSON。 如果我將回撥轉回帖子,它會像JSON一樣響應。

目標:如何使用HTTP GET獲取JSON而不是XML?

我已經有了相當多的谷歌搜索,這不是通常的嫌疑人,如缺少ScriptService或沒有在web.config中注冊處理程序。 它的行為就像腳本處理程序工廠只處理帖子? 請幫助我指出正確的方向!

服務器代碼:

namespace mynamespace
{
    /// <summary>
    /// Summary description for ServiceAddresses
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [System.Web.Script.Services.ScriptService]
    public class MyService : System.Web.Services.WebService
    {
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
        public string HelloWorld()
        {
            return "Hello World at " + DateTime.Now.ToLongTimeString();
        }
    }
}

客戶代碼:

function testhelloworld(postorget) {
    var webMethod = '/servicedir/MyService.asmx/HelloWorld';
    $.ajax({
        type: ('GET'===postorget)?'GET':'POST',
        url: webMethod,
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        data: "{}",
        success: function(msg) {
            $('#info').text(msg.d);
        },
        error: function(xhr, ajaxOptions, thrownError) {
            $('#info').text('Error: ' + xhr.responseText);
        }
    });
}

如果切換服務UseHttpGet =虛假和客戶端請求的POST工作正常 如果我使用GET,則返回XML。

Fiddler說請求是:

GET http://myserver/servicedir/MyService.asmx/HelloWorld?{} HTTP/1.1
Host: myserver
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.9) Gecko/20100824 Firefox/3.6.9 ( .NET CLR 3.5.30729)
Accept: application/json, text/javascript, */*
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
X-Requested-With: XMLHttpRequest
Referer: http://myserver/test/index.aspx

響應:

HTTP/1.1 200 OK
Server: Microsoft-IIS/5.1
Date: Thu, 14 Oct 2010 00:31:44 GMT
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Content-Length: 115

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World at 8:31:44 PM</string>

web.config的相關部分:

<webServices>
  <protocols>
    <add name="HttpGet"></add>
    <add name="HttpPost"></add>
  </protocols>
</webServices>
. . .
        <remove verb="*" path="*.asmx"/>
        <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
 . . .
  <remove name="ScriptHandlerFactory"/>
  <remove name="ScriptHandlerFactoryAppServices"/>
  <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

同樣的事情,但使用UseHttpGet = false重新編譯並通過POST請求工作。

Fiddler說POST請求是:

POST http://myserver/servicedir/MyService.asmx/HelloWorld HTTP/1.1
Host: myserver
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.9) Gecko/20100824 Firefox/3.6.9 ( .NET CLR 3.5.30729)
Accept: application/json, text/javascript, */*
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Content-Type: application/json; charset=utf-8
X-Requested-With: XMLHttpRequest
Referer: http://myserver/test/index.aspx
Content-Length: 2
Pragma: no-cache
Cache-Control: no-cache

{}

響應:

HTTP/1.1 200 OK
Server: Microsoft-IIS/5.1
Date: Thu, 14 Oct 2010 00:37:03 GMT
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private, max-age=0
Content-Type: application/json; charset=utf-8
Content-Length: 33

{"d":"Hello World at 8:37:03 PM"}

先發制人地回答非答案:

我想使用GET,因為我希望客戶端能夠緩存。

我知道有一些安全問題,例如發布在scott gu的博客上。

我知道我不能使用ASP.NET的腳本,只是自己動手,或試試Jayrock。 但是,我想了解為什么庫存ASP.NET ScriptHandler不起作用。

不確定asmx Web服務,但我們使用WCF並且它在該方法上使用這些屬性

[WebGet(RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM