Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax query works with dataType:'text' fails with dataType:'text/xml; charset=utf-8'

When I try to do an Ajax query with dataType of 'text/xml; charset=utf-8'... I get a parsererror.

  1. The xml response is valid xml
  2. The response header shows a Content-Type of 'text/xml; charset=utf-8'.
  3. It's not a cross domain request

These three problems were answers in other parsererror questions.

My ajax looks like this:

        $('#submitLogin2').click(function (e) {
            e.preventDefault();
            var formData = $('#loginForm2').serialize();
            var url = 'http://somewhere.com/Api2.0/Session_Create.aspx';
            $.ajax({
                url: url, type: "POST", dataType: 'text/xml; charset=utf-8',
                data: formData, contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
                success: function (data) {
                    $('#loginResult').html(data.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/ /g, '&nbsp;').replace(/\n/g, '<br />'));
                },
                error: function (textStatus, errorThrown) {
                    alert(errorThrown);
                    alert(JSON.stringify(textStatus));
                }
            });
        });

And the response is:

<Response><Error code='0'>Invalid User Name or Password</Error></Response>

It's great that the 'text' request works... but it would be nice to let Ajax parse the xml for me. Any ideas on how to get this to work?

like image 456
Brian Rice Avatar asked Nov 07 '15 01:11

Brian Rice


People also ask

What is dataType in ajax?

The dataType option specifies the type of response data, in this case it is JSON. The timeout parameter specifies request timeout in milliseconds. We have also specified callback functions for error and success. The ajax() method returns an object of jQuery XMLHttpRequest.

Which method is used on the returned object of ajax () method if the ajax call fails?

If an AJAX request fails, you can react to the failure inside the callback function added via the fail() function of the object returned by the $. ajax() function. Here is a jQuery AJAX error handling example: var jqxhr = $.

How can make ajax call in jQuery?

The ajax() method in jQuery is used to perform an AJAX request or asynchronous HTTP request. Parameters: The list of possible values are given below: type: It is used to specify the type of request. url: It is used to specify the URL to send the request to.


2 Answers

Looking at http://api.jquery.com/jQuery.ajax/#jQuery-ajax-settings - dataType "xml" is supported.

Changing your query to following should give you expected result:

url: url, type: "POST", dataType: 'xml',
like image 196
Mirec Miskuf Avatar answered Nov 15 '22 15:11

Mirec Miskuf


You have also to parse the XML response to process it as string with something like $.parseXML(data) or a XMLSerializer. I think this is even more important, hence the response dataType should be automatically determined by the MIME type.

like image 26
Noli Avatar answered Nov 15 '22 16:11

Noli