How to decode XML response from jQuery $ .ajax request in Firefox

I am trying to create an ajax request to a WebService that returns data with given parameters in XML. This seems to work in IE, but Firefox cannot decode the response. I can also view the response in Fiddler after decoding. Here is the code:

$(function() { $.ajax({ type: "GET", url: 'http:/localhost/webservice.asmx/GetTags?groupId=10', contentType: "text/xml; charset=utf-8", dataType: "xml", success: function(response) { $('#result').html('success',response); $(response).find("string").each(function() { $('#result').append($(this).text()); }); }, error: function(response) { $('#result').html('failure',response); } }); }); 

Can I specify which response to decode? Or any other way to make it work?

EDIT: @ Nikki9696 is not JSON encoded as the data is returned in XML.

@Oleg is a sample XML that I can see in the browser if accessing the web service through the URL looks like this:

 <?xml version="1.0" encoding="utf-8"?> <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/"> <string>tag 1</string> <string>tag 2</string> <string>tag 3</string> </ArrayOfString> 

Violinist in TextView returns and message

"The response is encrypted and may be required before decoding. Click here to convert."

After clicking on it, the same XML is displayed. I turn off dynamic compression of content in IIS, then XML is immediately visible in the script, but FF still can’t cope, so it excludes compression.

I played around a bit with the script, it seems that jQuery can by default or guess some parameters, so dataType, for example, is optional. With these settings, I get a success message, but he still does not know what to do with the data. I tried to set dataType to "jsonp", as suggested in some SS thread (I can’t find it at the moment, will bind it when I do this), and the error will change to missing ; before statement missing ; before statement , I think, because this is not a JSON object, but XML, Is there a way to force webservice to return JSON instead?

EDIT 2: I updated the url to reflect what actually happened. Sorry, I skipped this, making it impossible for anyone to notice it.

+6
jquery web-services
source share
2 answers

Since you are using a relative URL, such as '/webservice.asmx/GetTags?groupId=10' , you did not have problems with another domain. It seems to me that you should just fix litle your JavaScript code. For example, the following code

 $(function () { $.ajax({ type: "GET", url: '/WebService1.asmx/GetTags', contentType: "text/xml; charset=utf-8", data: {groupId:10}, success: function (response) { $('#result').html('success:'); $(response).find("string").each(function () { $('#result').append('<br />'+$(this).text()); }); }, error: function (response) { $('#result').html('failure:<br />' + response.responseText); } }); }); 

Works great in Internet Explorer, Firefox, and Google Chrome. If you need, I can publish a URL where you could download the entire working project of Visual Studio 2010.

UPDATED: To return JSON instead of XML from a web method, you can replace [ScriptMethod(UseHttpGet = true)] with the attribute [ScriptMethod (UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] (in .NET 4.0 you can do the same in different ways) and change the JavaScript code to the next

 $(function () { $.ajax({ type: "GET", url: '/WebService1.asmx/GetTagsJson', contentType: "application/json; charset=utf-8", data: { groupId: 10 }, //dataType: "xml", success: function (response) { $('#result').html('success:'); $(response.d).each(function () { $('#result').append('<br />' + this); }); }, error: function (response) { $('#result').html('failure:<br />' + response.responseText); } }); }); 
+2
source share

Short answer: I tried to make a call from another domain to my web service.

Here are some more details: In Firebug, looking at the XML tab, I noticed that the returned error was XML Parsing Error: no element found Location: moz-nullprincipal:{757cb587-20da-4d2f-bf80-e3b915a234d4} Line Number 1, Column 1: so I searched for this particular post and came across someone who had the same problem http://forum.jquery.com/topic/jquery-ajax-and-xml-issues-no-element-found . Here is the part that solved my problem:

Although the documentation is not clear, you cannot use AJAX calls to retrieve data from other domains.

It seemed to me that the reason why she worked in IE was because the first time he asked me if I would be allowed to make this unsafe call (or some), and he should have referred to making an ajax call to another domain . FF never told me about this and probably turned off the default call.

Making cross-domain compatible is another day's problem while it does everything I need.

+1
source share

All Articles