0

I have a code where i need to make cross domain requests to get the text/html as response. I've been using the JSONP method and was getting the results (json) before on some other program. When i run the program, console reports an error which is

 Uncaught SyntaxError: Unexpected token < 
 ?callback=jQuery17207555819984991103_1414157672145&_=1414157675836"

As you can see it has added an extra parameter value after the callback parameter which is causing an issue. When i see the response in Network tab, its pretty good and exactly what i wanted. I tested it on 4 cross domain links and that extra paramter is coming to all of them. Following is my code:

$.ajax({
                type: 'get',
                url: link,
                dataType: 'jsonp',
                success: function(dataWeGotViaJsonp){
                    alert(dataWeGotViaJsonp)
                    var len = dataWeGotViaJsonp.length;
                }
            });

The links I have passed to it:

 http://stackoverflow.com/questions/20944962/data-grid-view-update-edit-and-delete-in-vb-net-windows-form-that-using-multipl
 http://www.espncricinfo.com/pakistan-v-australia-2014/engine/match/727927.html
 http://pucit.edu.pk/

Response is good but due to that error, success callback isn't being called. Any solutions?

Ali Baig
  • 3,819
  • 4
  • 34
  • 47
  • 3
    Sounds like the response is not valid JSONP. JSONP is nothing but including a script element dynamically. I.e. the response must be valid JavaScript. HTML is not valid JS. What you are trying to do is simply impossible. – Felix Kling Oct 24 '14 at 13:50
  • 2
    you can't just pass any urls to use `jsonp`, the target resource must support `jsonp` else it is of no use. The links you have posted above does not support jsonp.... they will return a html content irrespective of the `dataType` used... which will result in the said error – Arun P Johny Oct 24 '14 at 13:51
  • JSONP is essentially a script injected to your page containing a function call to your receiving callback function whose argument is an object. You need to follow that format, or it won't be JSONP at all. – Joseph Oct 24 '14 at 13:53
  • set in ajax parameters: jsonpCallback: 'callback' – miglio Oct 24 '14 at 14:00

1 Answers1

2

"Uncaught SyntaxError: Unexpected token <" is an indication that the returned data is very likely HTML mark-up and not proper JSONP. In order to return HTML from a JSONP web service, you need something on the server that is wrapping the HTML in proper procedure call syntax. E.g.,

jQuery17207555819984991103_1414157672145 ("<div>... your HTML here ...</div>")

Since the HTML will likely have quote characters in it somewhere, you will need to escape, URL encode, UUEncode, or otherwise convert the HTML text to be an appropriate Javascript string, and then convert it back in the client.

As for the "_=1414157675836", this is being added to ensure the response is not cached. Unless your server's web service is rejecting the request because the "_" parameter is not recognized, this is a red herring. The problem is the bad JSONP syntax coming from the host.

cshotton
  • 2,784
  • 2
  • 14
  • 11