当前位置: 动力学知识库 > 问答 > 编程问答 >

Parsing the HTTP Response from an AJAX call into a jQuery object

问题描述:

I've been searching all over for a solution to this and maybe I'm just assuming that this is possible when it's really not, but here's the situation:

I have an AJAX request that I'm using to try and pull in an XML file from a public service -

var url = 'http://data.cocorahs.org/export/exportreports.aspx?ReportType=Daily&dtf=1&Format=XML&State=AZ&County=MR&ReportDateType=reportdate&Date=8/2/2016&TimesInGMT=False';

var xhr = $.ajax({

type: 'GET',

url: url,

dataType: 'jsonp'

});

Pretty standard stuff. You'll notice that I'm forcing the dataType to jsonp to get around the Cross-Origin policy even though the source is XML. Unfortunately, that causes a parsing error since jQuery wants to parse this as JSON data (which is normally what you'd want).

I can access that URL directly and my browser gets an XML file. When I do an AJAX request, the response headers give a 200 OK status and the response content is the actual XML (more than likely as a string since it's assumed as JSON data). How can I get this response into something usable? Is there any way to pull this as a jsonp but prevent jQuery from trying to parse it and manually parse it as XML data? I've tried to use the converters options to no avail.

var xhr = $.ajax({

type: 'GET',

url: url,

dataType: 'jsonp xml',

converters: {

'json xml': function(data){

return $.parseXML(data);

}

}

});

This just throws out an Uncaught SyntaxError: Unexpected token < error. How can I grab the response before it tries to get parsed and do something with it?

Update: After digging around a little, I was almost able to do what I wanted using Yahoo's Developer Console (https://developer.yahoo.com/yql/). How is Yahoo able to do this without getting around the Cross-Domain issues?

分享给朋友:
您可能感兴趣的文章:
随机阅读: