2010-10-14 30 views
5

Triệu chứng: Khi tôi thực hiện yêu cầu dịch vụ web (từ JQuery sử dụng .ajax, sang tệp ASP.NET .asmx), nếu nó được tạo bằng cách sử dụng GET thay vì POST, tệp .asmx luôn trả về XML thay vì JSON. Nếu tôi lật ngược cuộc gọi trở lại các bài đăng, nó sẽ phản hồi tốt như JSON.ASP.NET WebService nhầm lẫn trả về XML thay vì JSON chỉ khi sử dụng Http Get nhưng web.config được thiết lập đúng

Mục tiêu: Làm cách nào để nhận JSON thay vì XML, sử dụng HTTP GET?

Tôi đã có một chút công bằng về sự googling và không phải là những kẻ tình nghi thông thường như thiếu ScriptService hoặc không đăng ký trình xử lý trong web.config. Nó hoạt động giống như các nhà máy xử lý kịch bản chỉ làm việc trên các bài viết? Xin hãy giúp tôi đi đúng hướng ở đây!

đang Server:

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

Khách hàng mã:

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

trình tốt nếu tôi chuyển sang dịch vụ cho UseHttpGet = yêu cầu giả và khách hàng là POST. Gửi lại XML nếu tôi sử dụng GET.

Fiddler nói yêu cầu là:

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 

đáp ứng:

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> 

phần có liên quan của 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"/> 

điều chính xác tương tự nhưng biên dịch lại với UseHttpGet = false và yêu cầu qua POST hoạt động.

Fiddler nói POST yêu cầu là:

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 

{} 

đáp ứng:

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

Pre emptively trả lời các phi câu trả lời:

Tôi muốn sử dụng GET vì tôi muốn khách hàng để có thể cache.

Tôi biết rằng có những lo ngại về bảo mật với ví dụ: được đăng trên blog của scott gu.

Tôi biết tôi không thể sử dụng nội dung tập lệnh của ASP.NET và chỉ tự mình làm hoặc thử Jayrock. Nhưng, tôi muốn hiểu tại sao cổ phiếu ASP.NET ScriptHandler không hoạt động.

Trả lời

0

Không chắc về các dịch vụ asmx web, nhưng chúng tôi sử dụng WCF và nó hoạt động với những thuộc tính trên phương pháp

[WebGet(RequestFormat = WebMessageFormat.Json, 
ResponseFormat = WebMessageFormat.Json] 
Các vấn đề liên quan