When I try to do an Ajax query with dataType of 'text/xml; charset=utf-8'... I get a parsererror.
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, '&').replace(/</g, '<').replace(/>/g, '>').replace(/ /g, ' ').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?
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.
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 = $.
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.
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',
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With