Asmx web service - jQuery ajax post json (error 500) - CORS (Access-Control-Allow-Origin set to *)

This question seems to be one of millions of “solutions” with even more mixed results. So please cut me a little while I try to demonstrate what happens, what I use and how I tried to solve the problem.

IIS6 asp.net 2.0 is running in my test environment, I installed it using

'Access-Control-Allow-Origin' * 

I also installed verbs for .asmx extensions for

 "GET,HEAD,POST,DEBUG,OPTIONS" 

via jQuery v1.7.2 ajax () calls I can get xml files from the section and POST for the base service, which returns a string with the expected cross-domain of results. Therefore, I KNOW, at least in part, my cross-domain requests work.

My problem arises when I start expanding it and using json to send data to the service

I have a simple asmx webservice method:

  [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public SubResponse HelloWorldX(SubRequestXX SubReq) { SubResponse sr = new SubResponse(); sr.resultCode = "777"; sr.resultMsg = "hello - " + SubReq.EmailAddress; return sr; } 

It is very simple and not so much magic that I want to do is pass the SubRequestXX object, and for now just check it, and he answered.

 Using jQuery: $.ajax({ type: "POST", url: "http://somedomain/subscription/MyService.asmx/HelloWorldX", processData: false, data: '{ "SubReq" : {"EmailAddress":" user@test.com "} }', 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); } }); } 

Perhaps the answer lies there? but here is what Fiddler RAW data shows me using the same JS code from a page in the same domain (the result code is the result of 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":" user@test.ca "} } 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 - rob@test.ca "}} 

So, everything looks great and behaves as expected. Now, if I take the same html / js code, deploy it on another server and make the same request (cross-site) for the violinist’s testimonies (500 failed result code):

 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 

Many search results suggest a couple of changes to web.config. if I add the following to the system.web section:

  <webServices> <protocols> <add name="HttpGet"/> <add name="HttpPost"/> </protocols> </webServices> 

then my request looks like

 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 

And my ANSWER:

 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 by adding this section to my web.config, you did something because my results are slightly different. But I can not distinguish that.

Other solutions offer these web configuration settings, since I use asp.net 2.0, please note that I left them, as shown in all of the above results, the comments are originals from the creation of the project:

  <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 ... the big question ... Why are my cross-sites not working with the same code, despite all of the above? It looks like as soon as the request comes from another domain, it suffocates in JSON.

Any help would be appreciated for solving this mystery. I assume that this is either an IIS6 parameter (server) or my call to $ .ajax (), and I feel that it is probably something simple or small, but I can't seem to point a finger at it.

+6
source share
1 answer

I tried something similar through JSONP. I think I based the solution to this article.

JSONP + CORS

Another potential thing is that including JSON in a string in a WCF service usually doesn’t end well (I know this is not WCF, just saying that it could be the same problem). I usually do this:

 Using jQuery: $.ajax({ type: "POST", url: "http://somedomain/subscription/MyService.asmx/HelloWorldX", processData: false, data: JSON.parse('{ "SubReq" : {"EmailAddress":" user@test.com "} }'), 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); } }); } 

Using JSON.parse will also catch any bad JSON that goes through.

Hope this fixes your issue.

0
source

Source: https://habr.com/ru/post/926534/


All Articles