這個話題似乎是百萬「解決方案」中的一個,有更多混合的結果。所以請讓我有點鬆懈,因爲我會試圖展示正在發生的事情,我正在使用的以及我試圖解決問題的方式。asmx web service - jQuery ajax post json(500錯誤) - CORS(Access-Control-Allow-Origin設置爲*)
我的測試環境中運行IIS6 asp.net 2.0中,我已經將它設置了
'Access-Control-Allow-Origin' *
我也通過jQuery V1設定爲動詞的.asmx擴展
"GET,HEAD,POST,DEBUG,OPTIONS"
。 7.2 ajax()調用我可以從服務器獲取xml文件,並POST到基本服務,該服務返回一個字符串,其預期結果跨域。所以我知道至少部分我的跨域請求正在工作。
當我開始拓展上,並使用JSON將數據發佈到服務我的問題出現
我有一個簡單的ASMX Web服務方法:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public SubResponse HelloWorldX(SubRequestXX SubReq)
{
SubResponse sr = new SubResponse();
sr.resultCode = "777";
sr.resultMsg = "hello - " + SubReq.EmailAddress;
return sr;
}
很簡單,並沒有太大的魔力發生在那裏,我想要做的就是傳遞'SubRequestXX'對象,並且現在只需驗證它到達並響應。
Using jQuery:
$.ajax({
type: "POST",
url: "http://somedomain/subscription/MyService.asmx/HelloWorldX",
processData: false,
data: '{ "SubReq" : {"EmailAddress":"[email protected]"} }',
dataType: "json",
contentType: "application/json; charset=utf-8",
cache: false,
success: function(data, textStatus, jqXHR) {
ajaxSucccess3(data, textStatus, jqXHR);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log("Retrieve Hello Failed (AJAX Error): " + jqXHR.statusText + " | " + textStatus + " | " + errorThrown);
}
});
}
也許答案就在那裏?但這裏是提琴手的RAW數據顯示,我用從頁面完全相同的JS代碼在同一個域(成功 - 結果代碼200):
REQUEST:
POST http://somedomain/subscription/Service.asmx/HelloWorldX HTTP/1.1
Accept: application/json, text/javascript, */*; q=0.01
Content-Type: application/json; charset=utf-8
X-Requested-With: XMLHttpRequest
Referer: http://somedomain/subscription/subscription_-cors.html
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; 9LA)
Host: somedomain
Content-Length: 45
Connection: Keep-Alive
Pragma: no-cache
{ "SubReq" : {"EmailAddress":"[email protected]"} }
RESPONSE:
HTTP/1.1 200 OK
Date: Fri, 28 Sep 2012 16:59:15 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
X-AspNet-Version: 2.0.50727
Cache-Control: private, max-age=0
Content-Type: application/json; charset=utf-8
Content-Length: 147
{"d":{"__type":"SubService.DataObj.SubResponse","resultCode":"777","resultMsg":"hello - [email protected]"}}
所以一切看起來不錯,按照我們的期望。現在,如果我採取相同的HTML/JS代碼,部署在另一臺服務器上,並提出同樣的要求(跨站點)提琴手顯示(失敗結果代碼500):
REQUEST:
POST http://somedomain/subscription/Service.asmx/HelloWorldX HTTP/1.1
Accept: */*
Origin: http://www.crossdomainsite.com
Accept-Language: en-US
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; 9LA)
Host: somedomain
Content-Length: 0
Connection: Keep-Alive
Pragma: no-cache
RESPONSE:
HTTP/1.1 500 Internal Server Error
Date: Fri, 28 Sep 2012 16:58:56 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 4732
.....
Request format is unrecognized for URL unexpectedly ending in '/HelloWorldX'
.....
[InvalidOperationException: Request format is unrecognized for URL unexpectedly ending in '/HelloWorldX'.]
System.Web.Services.Protocols.WebServiceHandlerFactory.CoreGetHandler(Type type, HttpContext context, HttpRequest request, HttpResponse response) +405961
System.Web.Services.Protocols.WebServiceHandlerFactory.GetHandler(HttpContext context, String verb, String url, String filePath) +212
System.Web.Script.Services.ScriptHandlerFactory.GetHandler(HttpContext context, String requestType, String url, String pathTranslated) +47
System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) +193
System.Web.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +93
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
許多搜索結果顯示幾個網站的。配置更改。如果我以下內容添加到我的System.Web部分:
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
然後我的請求看起來像
POST http://somedomain/subscription/Service.asmx/HelloWorldX HTTP/1.1
Accept: */*
Origin: http://www.crossdomainsite.ca
Accept-Language: en-US
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; 9LA)
Host: somedomain
Content-Length: 0
Connection: Keep-Alive
Pragma: no-cache
我的回答:
HTTP/1.1 500 Internal Server Error
Date: Fri, 28 Sep 2012 17:37:00 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/plain; charset=utf-8
Content-Length: 461
System.InvalidOperationException: HelloWorldX Web Service method name is not valid.
at System.Web.Services.Protocols.HttpServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response)
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)
所以補充說,部分我的網頁。配置做了些什麼,因爲我的結果稍有不同。但我無法辨別出什麼。
其他的解決方案,因爲我使用asp.net 2.0表明這些web配置設置,注意我已經離開這在所有上述結果如圖所示,註釋掉那些從項目創建原件:
<httpHandlers>
<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"/>-->
<add path="*.asmx" verb="*" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<!--<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>-->
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<!--<add verb="GET,HEAD" path="ScriptResource.axd" validate="false" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>-->
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
</httpHandlers>
所以...大問題...儘管如此,我的跨站點請求爲什麼會使用相同的代碼失敗?似乎只要請求來自另一個域,它就會在JSON上窒息。
任何幫助將不勝感激解決這個謎。我假設它是一個IIS6(服務器)設置或我的$。ajax()調用,我覺得它可能是簡單或小的東西,但我似乎無法把它放在手指上。