JSONP - "Unable to use SyntaxError: Unexpected token"

Before I explain my problem, I would like to mention that I am naive on jsonp

. This is actually my first time trying to work with jsonp

.

Im using jquery ajax to drop data from a website.

my jquery code is below

$.fn.checkTPS = function(){

    return this.each(function(){
        var interval;

        $(this).on('keyup', function() {
            var api_key = 'asdfasfsadfsadfsad';
            var format = 'json';
            var username = 'dame@example.co.uk';

            var self = $(this);
            var selfValue;
            var feedback = $('.tps-feedback');

            if(interval === undefined){

                interval = setInterval(function(){

                    if(selfValue !== self.val()) {

                        selfValue = self.val();

                        if (selfValue.length > 9){
                            $.ajax({
                                url: 'https://www.selectabase.co.uk/api/v1/tps/' + selfValue + '/',
                                type: 'get',
                                dataType: 'jsonp',
                                data: {
                                    format: format,
                                    username: username,
                                    api_key: api_key
                                },
                                success: function(data) {
                                    console.log(data);
                                },
                                error: function() {

                                },
                                jsonp: 'jsonp'
                            });
                        }
                    }
                },3000);
            }
        });
    });
};

      

I want to host the service from selectabase.co.uk

, according to them this is how I should use the servicehttps://www.selectabase.co.uk/api/v1/tps/[number]/?format=json&username=[username]&api_key=[api key]

when i send request with ajax i get this error Uncaught SyntaxError: Unexpected token :

and on click opens {"ctps": false, "number": "1452500705", "resource_uri": "/api/v1/tps/01452500705/", "tps": false}

btw this is what i want but dont know what the error is unexpected token :

I copied the following link from the check item tab (you can see the image below). I think this is the call that was generated by jsonhttps://www.selectabase.co.uk/api/v1/tps/01452500705/?jsonp=jQuery17102731868715648129_14120077325500&format=json&username=dame40example.co.uk&api_key=asdfasfsadfsadfsad&_=14120077325500

I copied the link below from inspect element > source tab

in chrome .. I think I should add an image to correctly describe where this json data is and the link I copied from.

enter image description here

Hope I can manage to get my message across ... please help if you have an idea what I need to add ... Regards

+3


source to share


1 answer


format=json

in the query string should be format=jsonp

. The server is responding with JSON, but you are expecting a JSONP response. But I don't know what they support format=jsonp

, it's just a guess.



Alternatively, if this server supports CORS and allows requests from your origin, you can process JSON instead (just remove dataType: "json"

from the ajax

call). Remember, this will require the user to be using a browser that correctly supports CORS , which IE8 and IE9 do not. (They support CORS, but not through a regular object XMLHttpRequest

, and this is a browser inconsistency that jQuery doesn't flatten out for you. If you're looking, you might find "plugins" or similar that will handle it.)

+6


source







All Articles