JSONP: status code 200 OK and still returns $.Ajax(...error:Function()...)

Asked

Viewed 1,935 times

1

We’re having trouble making a request $.ajax, JSONP.
The problem is that even with the positive return (Status Code:200) the function shown below always returns the error check 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);
                }
            });

        });         
    });  

requisições
retorno da requisição e o retorno jquery que diz: jQuery210027153274044394493_1423956716453 was not called

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

  • 1

    This is because you are using an external domain without CORS (I think).

  • This will only work if the server responds with Content-type: application/javascript.

  • 1

    The answer seems common JSON, already tried with dataType: 'json'?

  • Your jsfiddle example: http://jsfiddle.net/o3tbgbvm/ Gives the following error message: Resource Interpreted as scripts but transferred with MIME type text/Plain.

  • bfavaretto, thank you for your attention. Using datatype as 'json' would not work because it is a cross-Domain request, q requires jsonp.

  • Qmechanic73, thanks in advance for your attention, even modifying the script according to your suggestion the return pointed by the console was: "myCallback was not called". I had already tried this approach ( and even then the 'bigown' denied me by claiming: "lack of research"...)

  • 2

    How do you know who has denied you and why? I, who am a moderator, don’t know. I don’t see anything to indicate who did it, or why.

  • 2

    @Milmen I did not deny your question. Least of all you, because the website does not allow negative people. I edited to improve the style of the question. I was helping you without changing the essence of the question. Worse, I don’t know why you decided to say that I claimed something. Where did you get it?

  • Ladies and gentlemen, I’m sorry for the inconvenience of the misunderstanding. It was just a misinterpretation here on my part. Sincere apologies and thanks to the effort of each to help in their own way.

Show 4 more comments

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 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 answer to be a Javascript script. Why? Precisely because the tag <script> is one of the exceptions to the Same Origin Policy (and therefore a domain can include scripts on another domain page). For this to work, however, it takes a "trick":

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

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

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

  2. The server prepares the JSON response, normal, only when sending, it does it with the content-type text/javascript (or application/javascript, but if I remember correctly this gives problem in some older browsers) and the most important: 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, all that will come is a normal Javascript object:

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

    that will not execute any code, nor will it be saved in any variable, and nothing will happen...

Apparently you modified the server to serve the content-type correct, but forgot to add the padding. That’s why jQuery - found that the script was loaded and executed correctly, but the function of callback was not called - accuses error.

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

  • mgibsonbr, thank you for your attention. Enlightening. I found an alternative to obtaining the data: Xdomain, in github.com/jpillora/xdomain, however, it only returns me data of type text. I tried to modify it to return me in json format but without success.

  • 1

    It’s not just making one JSON.parse(texto) in the data received?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.