Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use JSONP to load an html page

I'm trying to load an external page using JSONP, but the page is an HTML page, I just want to grab the contents of it using ajax.

EDIT: The reason why I'm doing this is because I want to pass all the user information ex: headers, ip, agent, when loading the page rather than my servers.

Is this doable? Right now, I can get the page, but jsonp attempts to parse the json, returning an error: Uncaught SyntaxError: Unexpected token <

Sample code:

$.post('http://example.com',function(data){
    $('.results').html(data);
},'jsonp');

I've set up a jsfiddle for people to test with: http://jsfiddle.net/8A63A/1/

like image 212
Korvin Szanto Avatar asked Sep 23 '11 16:09

Korvin Szanto


People also ask

What is JSONP used for?

JSONP is a method for sending JSON data without worrying about cross-domain issues. JSONP does not use the XMLHttpRequest object. JSONP uses the <script> tag instead.

What is difference between JSON and JSONP?

Json is stardard format that is human readable used to transmit information from one server to another server. Jsonp is a json with ability to transmit information to another domain. JSONP is JSON with padding, that is, you put a string at the beginning and a pair of parenthesis around it.

Can JSONP be used with Ajax?

JSONP has nothing to do with Ajax, since it does not use XMLHttpRequest. Instead, it dynamically inserts <script> tag into a webpage.

What is JSONP callback function?

jsonp is the querystring parameter name that is defined to be acceptable by the server while the jsonpCallback is the javascript function name to be executed at the client. When you use such url: url: 'http://url.of.my.server/submit?callback=? ' the question mark ?


4 Answers

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.

like image 144
c69 Avatar answered Sep 19 '22 15:09

c69


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.

like image 45
martosoler Avatar answered Sep 17 '22 15:09

martosoler


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 …

like image 38
vzwick Avatar answered Sep 21 '22 15:09

vzwick


If you really just want to employ the client to snag an HTML file, I suggest using flyJSONP - which uses YQL.. or use jankyPOST which uses some sweet techniques:

jankyPOST creates a hidden iframe and stuffs it with a form (iframe[0].contentWindow.document.body.form.name).

Then it uses HTML5 (watch legacy browsers!) webMessaging API to post to the other iframe and sets iframe's form elements' vals to what u specified.

Submits form to remote server...done.

Or you could just use PHP curl, parse it, echo it, so on.

IDK if what exactly ur using it for but I hope this helps.

ALSO... I'm pretty sure you can JSONP anything that is an output from server code. I did this with ClientLogin by just JSONPing their keyGen page and successfully consoleLogged the text even though it was b/w tags. I had some other errors on that but point is that I scraped that output.

Currently, I'm trying to do what you are so I'll post back if successful.

like image 45
Cody Avatar answered Sep 18 '22 15:09

Cody