Problems in the return of a JSONP

Asked

Viewed 207 times

1

People I’m trying to get a response from an API of mine as JSONP but not the right one. Code ajax is this:

$.ajax({
            type: 'GET',
            url: url,
            async: false,
            contentType: "application/json",
            dataType: 'jsonp',
            complete: function(data) {
                console.log(data);
            }
        });

Return is this of the console: inserir a descrição da imagem aqui

This is AJAX’s return response:

inserir a descrição da imagem aqui

How do I recover these values in Jquery?

Thank you

  • var codigo = data[0].codigo?

  • @Sergio gave the following error: Uncaught Typeerror: Cannot read Property 'codigo' of Undefined

  • Okay, and what gives console.log(typeof data, data);?

  • @Sergio gives the answer that is in the first image I sent there in doubt.

  • Instead of complete: function(data) { forehead with success: function(data) {

  • @Sergio the strange also is that it does not enter the Success only in error, even the header stating status code 200 as the first picture shows. I no longer know what I did wrong. The hard that for example if I use POSTMAN and access the same API address everything comes back right. It could be because I’m running locally and accessing an external URL?

  • And are you sure it’s jsonp? the answer seems to me json... testa dataType: 'json',

  • With JSON the following error: Xmlhttprequest cannot load http://testejn.hosting desites.ws/api/bairros/16. No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'http://localhost:8080' is therefore not allowed access. Try to access via POSTMAN this URL and you will see that this right.

Show 3 more comments

2 answers

0

Following example:

       $.ajax({
            type: 'GET',
            url: url,
            async: false,
            contentType: "application/json",
            dataType: 'jsonp',
            complete: function(data) {
                console.log(data[0].atributo);
            }
        });

Or if you returned a String as JSON:

       $.ajax({
            type: 'GET',
            url: url,
            async: false,
            contentType: "application/json",
            dataType: 'jsonp',
            complete: function(data) {
                var obj = jQuery.parseJSON( data );
                console.log(obj[0].atributo);                    
            }
        });

To get all attributes of each array just do a simple for.

  • None of the examples worked see the errors: In the first example of this error: Uncaught Typeerror: Cannot read Property 'attribute' of Undefined In the second example of the following error: Uncaught Typeerror: Cannot read Property '0' of null

  • friend look posted ai the AJAX debug images see if you can help me. Thank you

  • Substitute atributo attributes that come from the object, example: bairro, cod_cidade. The atributo which I put in my reply was just as an example. Use the first example I gave you that will work.

  • look I’m using your first example and the error that when I put console.log(data[0].code) for example is this: Uncaught Typeerror: Cannot read Property 'code' of Undefined and if I give a console only at Undefined’s date[0].

0

Usa contentType: "text/plain",.

var url = 'http://testejn.hospedagemdesites.ws/api/bairros/16';
$.ajax({
    type: 'GET',
    url: url,
    async: false,
    contentType: "text/plain",
    dataType: 'jsonp',
    success: function (data) {
        console.log(data[0]);
    }
});

jsFiddle: http://jsfiddle.net/9p65b280/1/

Browser other questions tagged

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