API Call: Uncaught SyntaxError: Unexpected token :

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'); }
            });

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

链接地址: http://www.djcxy.com/p/46024.html

上一篇: “未捕获的SyntaxError:意外的令牌:”JSONP响应

下一篇: API调用:未捕获SyntaxError:意外的标记: