0

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?

Derek
  • 112
  • 1
  • 9
  • No, that's not possible unless the **server** sets CORS headers. You can't set the datatype to JSONP when getting XML, and expect it to work cross-domain, it only does that for JSONP data, as that's not really an ajax request, it inserts a script tag instead, jQuery just masks it as an "ajax" request, and that won't work with an XML file. – adeneo Aug 02 '16 at 20:40
  • @adeneo - Right, and this particular server does not. Why am I getting the XML file as a response then when I inspect in the browser and why can't I do anything with that response? The data I'm looking for is right there, but I can't seem to do anything with it. – Derek Aug 02 '16 at 20:44
  • It's somewhat frustrating, the file is right there, but you can't get the data with ajax for security reasons, it's called the same-origin policy, and there's nothing you can do, other than getting the file serverside, and then doing that ajax request to your own server etc. – adeneo Aug 02 '16 at 20:47
  • @adeneo - See my update. How is Yahoo able to get around this same-origin policy, then? I know that the issue lies in same-origin which is why I'm forcing jQuery to grab the data as `jsonp`. What I'm not understanding is how do I grab the data before jQuery tries to parse it or prevent jQuery from parsing it... – Derek Aug 02 '16 at 21:00
  • Yahoo doesn't get around it, the policy isn't "in Yahoo", it's in your browser. The browser disallows cross origin content gotten with ajax, that doesn't send CORS header. You can't *force* anyone to send you JSONP. If it's XML, it's XML, not JSONP, which is a totally different data format. There is no way around this, either you control the server and send JSONP data or CORS headers, if you don't control the server, you can't get the data **with ajax**, it's that simple. – adeneo Aug 02 '16 at 21:19
  • Yahoo has a service where they do the request from the serverside, which doesn't have to follow the same-origin policy, as it's not a browser, and then Yahoo gets that data from **their own** server, which is the same-origin as the site, and they send that data to you **with** CORS header, or as JSONP etc. – adeneo Aug 02 '16 at 21:20
  • @adeneo - Yes, I understand how cross-origin policies work. That wasn't the question. The question was that, using Yahoo's dev console, I'm able to pull exactly the XML data I am looking to pull. I am also able to get the XML data as an HTTP response by forcing jQuery to pull it as `jsonp` data. My question is simply how do I do something with that data that is sent in the HTTP response instead of having jQuery try and process it automatically. Instead, I'd want to parse it myself. – Derek Aug 03 '16 at 21:31
  • Just set the datatype to `text` and jQuery gets it as plain text, and you can parse it yourself. – adeneo Aug 04 '16 at 22:28
  • @adeneo - If I set the datatype to `text`, then CORS kicks in (or doesn't, for that matter) and all browsers ignore the data. The only datatype that downloads the HTTP response with the XML contents is `jsonp`. – Derek Aug 08 '16 at 15:17

0 Answers0