Difficulty with AJAX and JSONP

2

I have a problem with the ajax return with jsonp, it returns json, but I can not work with it.

Code:

$.ajax({
    method: "GET",
    url: "http://minasul.tecnologia.ws/previsao/index.php?tipo=TODAS",  
    async: false,
    contentType: "application/json",
    dataType: 'jsonp',
    success: function(json) {
        //QUANDO USO O SUCCESS, ELE NÃO ENTRA AQUI NESSE BLOCO
        //QUANDO USO O COMPLETLE, ELE ENTRA, MAS NÃO CONSIGO TRABALHAR COM O JSON
    }
});

JSFiddle

    
asked by anonymous 13.08.2015 / 14:17

2 answers

0

On your page where you run select vc should give an echo jsonencode.Example:

 $query = 'SELECT * FROM TABELA';
echo json_encode($query->result());

You will soon be able to get the result in your ajax.

$.ajax({
    type: "POST",
    url: "http://minasul.tecnologia.ws/previsao/index.php?tipo=TODAS",  
    async: false,
    dataType: 'json',
    success: function(data) {
       console.log(data);
    }
});
    
13.08.2015 / 15:01
0

Friend, maybe your difficulty is similar to what I was having. Instead of using $.ajax use $.getJSON .

For some reason even I include in the Header the properties (Access-Control-Allow-Origin","*") and ("Access-Control-Allow-Methods", "GET, POST") yet it is in error.

Try this:

 $.getJSON('Endereco_do_endpoint', function (data) {
 
      //Percorrer pelos itens retornados
      $.each(data, function (key, item) {
        console.log(item.ID);
      });
 });
    
20.11.2018 / 21:07