Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONP Unexpected token <

I want to request a remote XML file. I have read JSONP is the only way to do this without writing server side code. I am trying to request an XML file using the following code

<html>
<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            $.ajax({
                url: 'http://www.w3schools.com/xml/note.xml',
                dataType: 'jsonp',
                success: function(dataWeGotViaJsonp){
                    console.log(dataWeGotViaJsonp);
                }
            });
        })
    </script>
</head>
<body>
</body>
</html>

I get the error:

Resource interpreted as Script but transferred with MIME type text/xml: "http://www.w3schools.com/xml/note.xml?callback=jQuery1620008313672617077827_1390902958529&_=1390902958534". jquery.min.js:18 Uncaught SyntaxError: Unexpected token <

Any help much appreciated.

like image 538
Alex Avatar asked Jun 01 '26 16:06

Alex


1 Answers

You are probably trying to obtain data by JSONP to get around the same-origin policy.

But this also means you actually need to get JSONP, not XML.

But you can't just change the dataType to xml because you are not allowed because of the above policy.

So the server needs to support JSONP output.

solution:

You do need to make some serverside code though.

You can create a serverside script which can access xml by for example cUrl, and convert it to jsonp.

for example:

www.yourserver.com/xmltojson.php?url=http://www.w3schools.com/xml/note.xml

This script will convert the xml to json and you can just get it with your ajax call. (because this conversion is on your own server, no jsonp is needed but regular json will do.

Edit:

Found a tutorial for you : http://weedygarden.net/2011/01/consuming-remote-xml-as-jsonp/

like image 63
Timmetje Avatar answered Jun 03 '26 14:06

Timmetje