我的测试环境中运行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部分:
然后我的请求看起来像
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配置设置,注意我已经离开这在所有上述结果如图所示,注释掉那些从项目创建原件:
所以...大问题...尽管如此,我的跨站点请求为什么会使用相同的代码失败?似乎只要请求来自另一个域,它就会在JSON上窒息。
任何帮助将不胜感激解决这个谜。我假设它是一个IIS6(服务器)设置或我的$。ajax()调用,我觉得它可能是简单或小的东西,但我似乎无法把它放在手指上。