I’m doing a GET with ajax using dataType jsonp to bring in a small xml file from a server on another domain which I don’t own and can’t change. Any dataType other than jsonp fails with a “Origin localhost is not allowed by Access-Control-Allow-Origin. Fine.
The problem is that the server returns the same xml response when I say it’s jsonp and jQuery doesn’t seem to like this. dataFilter doesn’t seem to help as the instant I refer to the returned data I get an “Uncaught SyntaxError: Unexpected token <” error. Maybe there’s some other way to use the dataFilter setting?
My code is reproduced below:
$(document).ready(function(){
$.ajax({
type: "GET",
url: "https://b2b.firstenergycorp.com/invoke/ ... very long url... ",
dataType: "jsonp",
dataFilter: function(data, type){
if(type == "xml") alert("returned xml!");
var newdata = data.replace(/</g, '{'); // Firebug complains about this
newdata = newdata.replace(/>/g, '}');
return newdata;
},
jsonp: "callback",
jsonpCallback: "jsonpcallback"
});
function jsonpcallback(returndata) {
alert( "data returned: " + returndata );
};
});
the answer came in another stackoverflow question:
Jquery success function not firing using JSONP
“… In hindsight, the solution probably should have been more obvious than it was, but you need to have the web-response write directly to the response stream. Simply returning a string of JSON doesn’t do it, you need to someone construct it and stream it back.