Cross origin error in ajax call

162 views Asked by At
function getInfo()
{

    var URL="https://api-oauth2.mendeley.com/oauth/authorize?client_id=374&redirect_uri=https://localhost/api/mendeley/mendeley.php&response_type=code&scope=all&JSON=1";
    $.ajax({
        url: URL,
        type: 'get',
        dataType: "jsonp",
        jsonpCallback: "https://192.168.2.210/api/mendeley/mendeley.php",
        converters: {
            'text json': true
        },
        success: function (data) {
            console.log(data.content);


        },
        error: function(e) {
            console.log(e);
        }
    });
}

when I call this function this gives me error
SyntaxError: syntax error

1

There are 1 answers

2
Quentin On

You have several problems.


From the jQuery documentation:

jsonpCallback
Type: String or Function()
Specify the callback function name for a JSONP request. This value will be used instead of the random name automatically generated by jQuery. It is preferable to let jQuery generate a unique name as it'll make it easier to manage the requests and provide callbacks and error handling. You may want to specify the callback when you want to enable better browser caching of GET requests. As of jQuery 1.5, you can also use a function for this setting, in which case the value of jsonpCallback is set to the return value of that function.

https://192.168.2.210/api/mendeley/mendeley.php is not a valid JavaScript function name.


If I visit https://api-oauth2.mendeley.com/oauth/authorize?client_id=374&redirect_uri=https://localhost/api/mendeley/mendeley.php&response_type=code&scope=all&JSON=1 or https://api-oauth2.mendeley.com/oauth/authorize?client_id=374&redirect_uri=https://localhost/api/mendeley/mendeley.php&response_type=code&scope=all&JSON=1&callback=something, I get an HTML document prompting me to log in.

You need to hit a real JSONP endpoint to use JSONP.