Use JSONP to load an html page

后端 未结 6 760
不思量自难忘°
不思量自难忘° 2020-11-27 17:13

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: T

相关标签:
6条回答
  • 2020-11-27 17:27

    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 …

    0 讨论(0)
  • 2020-11-27 17:31

    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.

    0 讨论(0)
  • 2020-11-27 17:34

    First, call the AJAX URL manually and see of the resulting HTML makes sense.

    Second, you need to close your DIV in your fiddle example.

    0 讨论(0)
  • 2020-11-27 17:35

    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.

    0 讨论(0)
  • 2020-11-27 17:37

    I don't think this is possible. JSONP requires that the response is rendered properly.

    If you want another solution, what about loading the url in an iframe and trying to talk through the iframe. I'm not 100% positive it will work, but it's worth a shot.

    0 讨论(0)
  • 2020-11-27 17:42

    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.

    0 讨论(0)
提交回复
热议问题