1
votes

I am running an ajax call to get some Data from an API. The call itself works fine, I get 200 (OK) as a response. Also I can See the JSON Data in my Dev Tools. But in the end the alert is "error" and the I have a "Uncaught SyntaxError: Unexpected token :" in my Console.

What going wrong there?

$.ajax({
                url: 'my url',
                type:'GET',
                xhrFields: {
                    withCredentials: true
                },
                cache: false,
                dataType: 'jsonp',
                contentType: 'application/x-www-form-urlencoded',

                // headers: {
                //     'Authorization': 'Basic ' + btoa('uberall:cat-aspirin-window-cake')
                // },
                beforeSend: function (xhr) {
                    xhr.setRequestHeader('Authorization', 'Basic ' + btoa(user + ":" + pw));
                },

                success: function(text) { alert('success'); },
                error: function (text) { alert('error'); },
                complete: function(text) { alert('complete'); }
            });
1
Did you check request content with tools like Advanced Rest API, Fiddler - Amit Kumar Singh

1 Answers

0
votes

If you are using jsonp , you should have a callback method implemented, it seems you are missing it.

You may first try to print the error by replacing the following function to error field, see if it is missing the jsonpCallback or other error

error:function(jqXHR, textStatus, errorThrown){
    console.log("jqXHR",jqXHR);
    console.log("textStatus",textStatus);
    console.log("errorThrown",errorThrown);
}

If it is jsonp you should check server side see what should be the callback, but you do not know , you may try add as many server set this as default

Note: this is not a good pratice

jsonp: 'callback'

and also ref to JSONP NOT CALL