-2

Whenever I do the following ajax request to get the html of a page:

      $.ajax({
            type: 'GET',
            dataType:"jsonp",
            url: link,
            success: function(response){console.log(response)},
            async:true //asynchronous request
       });

I get the error: "Uncaught SyntaxError: Unexpected token <"

Why is this and how can I solve it?

Alvaro Silvino
  • 9,441
  • 12
  • 52
  • 80
jgozal
  • 1,480
  • 6
  • 22
  • 43
  • 1
    Is "the html of a page" in `jsonp` format? – Sparky Oct 02 '15 at 14:27
  • not sure. I'm sorta new to jsonp format requests. Is there any way to change it to jsonp format in my request? If not, how else could I get the html of the page without encountering an Access-Control-Allow-Origin error? – jgozal Oct 02 '15 at 14:28
  • The server you are calling is not returning JSONP. You can not just set JSONP and expect it to work, the page you are calling needs to support it. – epascarello Oct 02 '15 at 14:38
  • how can i know the datatype that it returns? – jgozal Oct 02 '15 at 14:38
  • You can always try catch and handle it but find out why its happening. – Ali Gajani Oct 02 '15 at 14:39
  • Look at the request in the network tab and see what it is returning. – epascarello Oct 02 '15 at 14:39
  • text/html? HTTP/1.1 200 OK Date: Fri, 02 Oct 2015 14:42:22 GMT Server: Apache Vary: X-Forwarded-Proto,Accept-Encoding Last-Modified: Wed, 30 Sep 2015 08:54:19 GMT ETag: "701-520f313786a54-gzip" Accept-Ranges: bytes Content-Encoding: gzip Cache-Control: no-cache Expires: -1 P3P: CP="NON" X-UA-Compatible: IE=Edge Pragma: no-cache Content-Length: 773 Keep-Alive: timeout=8, max=63 Connection: Keep-Alive Content-Type: text/html; charset=utf-8 – jgozal Oct 02 '15 at 14:44
  • **If you have no control of the target server, you can't fix this problem without server-side code.** – Kevin B Oct 02 '15 at 15:24

1 Answers1

0

In the dataType attribute you're telling the function to expect a JSON response:

dataType:"jsonp"

The error you're getting is the error that occurs when your function expects JSON but instead gets HTML. Thus Unexpected token < is what is described by the error as the function fails to parse the HTML response (which unlike JSON usually starts with the '<' character).

Alternatex
  • 1,505
  • 4
  • 24
  • 47
  • I see. if I put html I get the following error and I'm not too sure about how I would solve it: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. – jgozal Oct 02 '15 at 14:53
  • Right, so either you have to follow the same origin policy, or you have to use proper jsonp. If you don't control the target server, you can't fix either of those problems with cilent-side javascript. – Kevin B Oct 02 '15 at 14:55
  • @jgozal Read what Kevin B said and also check out [this answer](http://stackoverflow.com/a/20035319/1200185). – Alternatex Oct 02 '15 at 14:56
  • I tried using CORS as well and it yielded the same error. Could it be that the server doesn't handle CORS requests? I am also trying another workaround. Say I load this page through an iFrame. Could I just get the html of the page like this?: $("#iFrame").contents().find("html").html() – jgozal Oct 02 '15 at 15:08
  • @jgozal Maybe you should open a new question and ask about what you want to do specifically. Your issue is no longer related to this question. – Alternatex Oct 02 '15 at 15:13
  • it is very related. I am trying to get the html of an external page as explained in the first sentence of my question. If anybody can find another way to do it better than through an ajax request, that works too – jgozal Oct 02 '15 at 15:17
  • @jgozal The title of this question refers to a JavaScript error, which doesn't really capture the bigger picture of what you're attempting to do.. Getting the HTML of a different-domain page solely through JavaScript is not going to work. Check [this](http://stackoverflow.com/questions/11023051/how-to-get-the-html-of-a-div-from-a-different-page-with-ajax) approach. – Alternatex Oct 02 '15 at 15:23