XML parsing error: element not found

I have an ASP.Net 4.0 Web Service method that returns a well-formed XML document. I successfully display XML in a browser locally and once deployed to a production server.

When I try to call a method through jQuery ajax, I get an error:

XML parsing error: no items were found. Location: moz-nullprincipal: {6c0c99b3-0fed-454f-aa6e-e0fca93a521c} Line number 1, column 1:

$.ajax( { url: 'http://mywebservice.com/WebService/Service.asmx/UserData', type: 'GET', contentType: "text/html; charset=utf-8", dataType: "xml", data: 'authorizedId=1234&authorizedUser=Test&authorizedCode=xyz', 'success': function (data) { $('#XMLContent').html(data.responseText); }, 'error': function (xhr, status) { alert(status); }, 'complete': function (xhr) { } }); 

I tried changing the contentType, but the same results.

However, I can make a call in C # like this, and I get my well-formed XML:

 XmlDocument document = new XmlDocument(); document.Load("http://mywebservice.com/WebService/Service.asmx/UserData?authorizedId=1234&authorizedUser=Test&authorizedCode=xyz"); ViewData["XMLData"] = document.OuterXml; 

In my web.config web service:

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

Thanks...

+4
source share
2 answers

If the web service is not in the same domain as the page, you cannot use AJAX calls to retrieve data from other domains.

You can create a proxy web service in your application that calls your external web service and then calls your own proxy server from AJAX / jQuery.

http://forum.jquery.com/topic/jquery-ajax-and-xml-issues-no-element-found

Hope that helps

+2
source

Thanks bgs264 ...

Now on my aspx page:

 $.ajax( { url: '/Home/WebService', type: 'GET', contentType: "text/html", dataType: "html", data: 'authorizedId=1234&authorizedUser=Test&authorizedCode=xyz', 'success': function (data) { alert(data); $('#XMLContent').html(data); }, 'error': function (xhr, status) { alert(status); }, 'complete': function (xhr) { } }); 

In my MVC controller:

 public ActionResult WebService(string authorizedId, string authorizedUser, string authorizedCode) { XmlDocument document = new XmlDocument(); document.Load("http://mywebservice.com/WebService/Service.asmx/UserData?authorizedId=" + authorizedId + "&authorizedUser=" + authorizedUser + "&authorizedCode=" + authorizedCode); ViewData["XMLData"] = document.OuterXml; return PartialView(); } 
0
source

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


All Articles