How can I know if a server allows JSONP or CORS

1

I want to use an API from a Wallpapers page but I run into restricted ajax domains. Investigate the error and it can be solved by using JQuery JSONP to take the information returned by the API. This is my request.

$.ajax({
        url: endpoint,
        type: 'GET',
        dataType: 'jsonp',
        data: { 'auth': apikey ,
                'method':'search',
                'term': keyword},
        success: function(data){
            console.log(data);
        }
    })

I also tried this way

$.getJSON(urlsearch+"?callback=?",function(result){
        console.log(result);
    })

It does not work in either of the two ways. I already checked that the API works correctly, This is the error that appears in firebug.

I read that maybe the server does not support JSONP or CORS and that a solution can be to create a PHP script that obtains the information and transfers it to javascript. But I'm not sure it's the best way to do it. Does anyone know of any solution?

Thanks in advance.

    
asked by Edwin V 01.10.2016 в 00:40
source

1 answer

2
  • If the page has an API, then the most common is that it has the CORS activated. However, the fact that you have it does not mean that you can connect if it is not public. In this case, you would need to authenticate yourself (username / password, token, key, etc.)
  • If you do not know the point 1 , then you make a simple request to the server to analyze the response. If you do not support CORS you will see it in the answer.
  • Depending on what your code shows, you are authenticating to the server with a key ( 'auth': apikey ). And this is reflected in the console, since you're getting an answer (you can see that an array of objects with data like width and height arrives).

    The message you get:

      

    SyntaxError: missing; before statement

    Probably because:

      

    JSON! = JSONP

    JSON is not the same as JSONP . A response JSONP is basically a script containing a function execution (default). An example of a JSONP response:

    functionPredeterminada({
      wallpaper: 'http://hdwallpapers.in?abc=14578',
      width: 2560,
      height: 1600
    })
    

    Conclusion

    What you have to do is change jsonp by json in order to have a JSON valid.

    $.ajax({
            url: endpoint,
            type: 'GET',
            dataType: 'json',
            data: { 'auth': apikey ,
                    'method':'search',
                    'term': keyword},
            success: function(data){
                console.log(data);
            }
        })
    
        
    answered by 01.10.2016 / 01:20
    source