JSONP: status code 200 OK and even then returns $ .Ajax (... error: function () ...)

1

We're having a hard time making a request $.ajax , JSONP. The problem is that even with positive feedback (Status Code: 200) the function shown below always returns the error-checking result as positive.

    $(document).ready(function() {
        $(document).on('click', '#checar', function(){
            $.ajax({
                type:     'GET',
                url:      'http://sinpesq.mpa.gov.br/rgp/web/sargp/index.php/atividade_pesca_profissional/atividade/RegularidadePescador',
                data:     {cpf: '37430564291', dtnascimento: '26-02-1970'},
                dataType: 'jsonp',

                error: function(xhr, status, error) {
                  console.log(error.message);
                },

                success: function(data){
                    console.log(data);
                }
            });

        });         
    });  


And nothing is returned. How to recover JSON data from this request?

    
asked by anonymous 15.02.2015 / 01:21

1 answer

3

JSONP is different from JSON. JSON is a data serialization format, when you make a request waiting for a JSON the server must return a text content. And in fact, that's what your server is returning.

[{bla},{bla},{bla}]

JSONP (JSON with padding ), on the other hand, expects the response to be a JavaScript script. Because? Precisely because the <script> tag is one of the exceptions to the Same Source Policy (and therefore a domain may include scripts from another domain). For this to work, however, a "trick" is needed:

  • The browser creates a script element, placing the URL to be queried and an additional parameter specifying the name of a function . Example:

    <script type="text/javascript" src="http://dominio/url?callback=myCallback">
    

    ( myCallback is just an example - any other function name could be used)

  • The server prepares the JSON response, normal, but at the time of sending, it does it with content-type text/javascript (or application/javascript , but if I remember correctly this gives problem in some older browsers) and most importantly: putting a padding :

    myCallback([{bla}, {bla}, {bla}]);
    

    Without this myCallback( at the beginning and ); at the end the JSONP call does not work - after all, everything that is going to come is a normal JavaScript object:

    [{bla}, {bla}, {bla}]
    

    It will not run any code, nor will it be saved in any variable, and nothing will happen ...

  • You apparently modified the server to serve the correct content-type , but forgot to add padding . This is why jQuery - noting that the script was loaded and executed correctly, but the callback function was not called - acknowledges the error.

    The solution is to modify the server to do the padding (remember: it is to use the name set in the parameter query string , not necessarily myCallback ). Now, if the server is not yours, but third-party (so you can not modify it), I'm afraid your only output is to use a proxy (for example, causing your own server access the third-party server and return the result to the client). In that case, you will not need to mess with JSONP (nor CORS) - you can just use JSON and you're done ...

        
    15.02.2015 / 03:51