Problems with JSON on the server

Asked

Viewed 537 times

1

Hello

I’m having trouble with the JSON when I climb it to the server..

Example of one of my code they use JSON:

        $("input[name=people]").keyup(function(){
            if($(this).val() != ''){
                $.ajax({
                    url: "<?php echo site_url('/StockController/searchPeople')?>",
                    type: "POST",
                    cache: false,
                    data: {name_people: $(this).val()},
                    success: function(data){
                        $('#loadPeople').html("");
                        var obj = JSON.parse(data);
                        if(obj.length>0){
                            try{
                                var items=[];   
                                $.each(obj, function(i,val){                                            
                                    items.push($("<option  id="+ val.id_people +">"+ val.name +"</option>"));
                                }); 
                                $('#loadPeople').append.apply($('#loadPeople'), items);
                            }catch(e) {     
                                alert('Ocorreu algum erro ao carregar os Fornecedores!');
                            }       
                        }else{
                            $('#loadPeople').html($('<span/>').text("Nenhum Fornecedor encontrado!"));      
                        }       
                    },
                    error: function(data){
                        alert("Ocorreu algum erro ao carregar os Fornecedores");
                    }
                });
            }else{
                $('#loadPeople').html(" ");
            }
        });

On the local server it runs smoothly, but when I go online the code stops working, and returns the following error on the console:

Uncaught Syntaxerror: Unexpected token in JSON at position 0

Does anyone know what might be going on?

  • you have how to post what comes out on your console.log() to the JSON you want to handle?

  • [{"id_product":"142","name_product":"Carregador de Celular Gen\u00e9rico","unit_price":"10.00"},{"id_product":"143","name_product":"Arroz Itatibaia","unit_price":"4.00"},{"id_product":"145","name_product":"Feij\u00e3o","unit_price":"10.99"},{"id_product":"146","name_product":"pao","unit_price":"12.00"}]

  • This.log() console is from the server or localhost?

2 answers

1

Try to change:

url: "<?php echo site_url('/StockController/searchPeople')?>",

for:

url: "<?php echo base_url('StockController/searchPeople')?>",

If it does not work post the searchPeople method();

1

Your server’s problem may be an access restriction to the JSON output. In php, you can do this to release the request:

header("Access-Control-Allow-Origin: *");

Although the most appropriate and safe way to do this is by passing only domains that have valid permission for the request:

$request = $_SERVER['SERVER_NAME'];
if (in_array($request, array('siteA.com.br', 'siteB.com.br'))) {
    header("Access-Control-Allow-Origin: $request");
}

Browser other questions tagged

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