Dieses Thema scheint eins mit einer Million "Lösungen" mit noch gemischten Ergebnissen zu sein. Bitte schneiden Sie mich etwas durch, da ich versuchen werde zu demonstrieren, was vor sich geht, was ich benutze und wie ich versucht habe, das Problem zu lösen.asmx Webdienst - jQuery ajax nach json (500 Fehler) - CORS (Zugriffssteuerung-Zulassen-Ursprung ist auf * gesetzt)
Meine Testumgebung läuft IIS6 asp.net 2.0, ich habe es eingerichtet mit
'Access-Control-Allow-Origin' *
ich auch die Verben für .asmx Erweiterungen
"GET,HEAD,POST,DEBUG,OPTIONS"
über jQuery v1 gesetzt haben. 7.2 AJAX() -Aufrufe Ich bin in der Lage, XML-Dateien vom Server abzurufen, und POST zu einem Basisdienst, der eine Zeichenfolge mit erwarteten Ergebnissen über die Domäne zurückgibt. Daher weiß ich zumindest teilweise, dass meine domänenübergreifenden Anfragen funktionieren.
Mein Problem entsteht, wenn ich auf das erweitern beginnen, und verwenden Sie json Daten an den Service
Ich habe eine einfache asmx WebService-Methode zu schreiben:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public SubResponse HelloWorldX(SubRequestXX SubReq)
{
SubResponse sr = new SubResponse();
sr.resultCode = "777";
sr.resultMsg = "hello - " + SubReq.EmailAddress;
return sr;
}
Sehr einfach, und nicht viel Magie Dort passiert, alles, was ich tun möchte, ist 'SubRequestXX' Objekt zu übergeben, und für jetzt einfach zu validieren es dort angekommen und zu reagieren.
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);
}
});
}
Vielleicht liegt die Antwort da? aber hier ist es, was Fiedler RAW-Daten zeigt mir genau die gleiche JS-Code von einer Seite auf der gleichen Domain mit (Erfolg - Ergebniscode 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]"}}
So sieht alles gut aus und verhält sich wie erwartet. Nun, wenn ich nehme die gleiche html/js Code, auf einem anderen Server installiert und macht die gleiche Anfrage (Cross-Site) Fiedler zeigt (nicht Ergebniscode 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
Viele Suchergebnisse deuten darauf hin, ein paar Web. Konfigurationsänderungen wenn ich folgendes meinen system.web Abschnitt hinzufügen:
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
dann sieht meine Anfrage wie
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
Und meine Antwort:
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)
So fügen hinzu, dass Abschnitt auf meine Web. config hat etwas gemacht, da meine Ergebnisse etwas anders sind. Aber ich kann nicht unterscheiden was.
Andere Lösungen empfehlen, diese Einstellungen Web-Config, da ich asp.net bin mit 2.0 stelle ich diese verlassen haben, wie bei allen obigen Ergebnisse gezeigt, kommentierte heraus diejenigen sind Originale aus der Projekterstellung:
<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>
So ... die große Frage ... Warum scheitern meine Cross-Site-Anfragen mit dem gleichen Code, trotz all der oben genannten? Scheint so, als ob die Anfrage von einer anderen Domain kommt, drosselt sie JSON.
Jede Hilfe würde sehr geschätzt, dieses Geheimnis zu lösen. Ich nehme an, es ist entweder eine IIS6 (Server) Einstellung oder mein $.Ajax() Anruf und ich fühle mich wie es ist wahrscheinlich etwas einfach oder klein, aber ich kann nicht scheinen, meinen Finger darauf zu legen.