2010-10-14 15 views
5

Belirti: Bir web hizmeti isteği yaptığınızda (JQuery kullanarak .ajax, ASP.NET .asmx dosyasına), POST yerine GET kullanılarak yapılırsa, .asmx dosyası her zaman JSON yerine XML döndürür. Aramayı tekrar yayınlara çevirirseniz, JSON kadar iyi yanıt verir.ASP.NET WebService yanlışlıkla yalnızca Http kullanıldığında JSON yerine XML döndürüyor Get ancak web.config doğru ayarlanmış

Hedef: HTTP GET kullanarak XML yerine JSON nasıl alabilirim?

Zaten epeyce uğraştım ve ScriptService eksik veya web.config dosyasında işleyici kaydetme gibi olağan şüpheliler değil. Script işleyicisi fabrikası sadece yazılarda çalışıyormuş gibi davranıyor mu? Lütfen bana doğru yönde bu noktada gelin!

Sunucu kodu:

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(); 
     } 
    } 
} 

Müşteri kodu:

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

İşleri ince Ben POST olarak UseHttpGet = false ve müşteri isteklerine hizmet geçerseniz. GET'i kullanırsam XML gönderir.

Kemancı isteği olduğunu söylüyor:

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 

Yanıt:

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> 

İlgili web.config parçaları:

<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"/> 

Aynı kesin bir şey ama UseHttpGet = false ve isteyerek ile yeniden derlenmesine POST ile çalışır.

Kemancı POST isteği olduğunu söylüyor:

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 

{} 

Yanıt:

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"} 

Öncesi emptively yanıtlayan olmayan cevaplar: Ben istemcileri istiyorum çünkü GET kullanmak istediğiniz

önbellek yapabilme

Güvenlik endişelerinin, örn. Scott gu'nun blogunda yayınlandı.

ASP.NET'in komut dosyalarını kullanamayacağımı ve sadece kendim yapamayacağımı veya Jayrock'u deneyebileceğimi biliyorum. Ancak, stok ASP.NET ScriptHandler neden çalışmıyor anlamak istiyorum.

cevap

0

asmx web hizmetleri hakkında emin değil, ama biz WCF kullanmak ve yöntem üzerinde bu özelliklere çalışır

[WebGet(RequestFormat = WebMessageFormat.Json, 
ResponseFormat = WebMessageFormat.Json] 
İlgili konular