Use JSONP to load an html page Use JSONP to load an html page ajax ajax

Use JSONP to load an html page


http://en.wikipedia.org/wiki/JSONP#Script_element_injection

Making a JSONP call (in other words, to employ this usage pattern), requires a script element. Therefore, for each new JSONP request, the browser must add (or reuse) a new element—in other words, inject the element—into the HTML DOM, with the desired value for the "src" attribute. This element is then evaluated, the src URL is retrieved, and the response JSON is evaluated.

Now look at your error:

Uncaught SyntaxError: Unexpected token <

< is the first character of any html tag, probably this is the start of <DOCTYPE, in this case, which is, of course, invalid JavaScript.

And NO, you can't use JSONP for fetching html data.


I have done what you want but in my case I have control of the server side code that returns the HTML.So, what I did was wrapped the HTML code in one of the Json properties of the returned object and used it at client side, something like:

callback({"page": "<html>...</html>"})

The Syntax error you are facing it's because the library you're using expects json but the response is HTML, just that.


I've got three words for you: Same Origin Policy

Unless the remote URL actually supports proper JSONP requests, you won't be able to do what you're trying to. And that's a good thing.

Edit: You could of course try to proxy the request through your server …