JSON transfer to Codeigniter via AJAX

Asked

Viewed 2,128 times

5

I’m not able to pass a string in the format JSON for a controller of Codeigniter via AJAX.

A string JSON:

var avaliacao= {"avaliacao":[{"pergunta":"Qual sua idade?"}[{"resposta_certa":"99","resposta_err1":"11","resposta_err2":"15",
"resposta_err3":"14","resposta_err4":"27"}]]}

THE JS:

var controller = 'endereco/controller';
var base_url = 'dominio/do/site';

function grava_avaliacao(){ 

    jQuery.ajax({
        url : base_url + '/' + controller + '/add',
        type : 'POST', 
        data : avaliacao,
        dataType: 'json',
        success : function(data){ 
                        alert(data);
                    }
    });
}

The controller:

public function add(){
    var_dump(json_decode($this->input->post('avaliacao')));
}
  • Is this json correct? Try jsonlint.com or others to test, this could be it.

1 answer

3


Use JSON.stringify() to convert your object into a Json string in the attribute data of function ajax.

The parameter data accepts a string or an object, but it does not automatically convert to Json but to the format of query string. It’s a common confusion.

Example:

jQuery.ajax({
    url : base_url + '/' + controller + '/add',
    type : 'POST', 
    data : { 'avaliacao': JSON.stringify(avaliacao) },
    dataType: 'json',
    success : function(data){ 
                    alert(data);
                }
});

Update

According to the comments, there was also a problem in the URL, because the controller method was not even executed. So, one should always check whether the URL used in Ajax commands is correct, especially when using frameworks that abstract the path.

  • I changed the js, but I have no return.

  • @Away I adjusted the code. See if it works now. I forgot that the parameter needs a name.

  • I still do not receive return. I changed the code, sending but I have no return.

  • @Away If you do var_dump($this->input->post('avaliacao'));, what appears on exit?

  • I still have nothing. Shipping and I have no feedback, I checked the call from the function and it’s working. I believe the problem is between the controller and js.

  • @Away Just so I understand well. Javascript code is executed and a request arrives on the server. The var_dump runs, but displays empty or null content. That’s right?

  • The request is not even coming to the server, if it were returning me at least one null content...

  • @Away If you’re not coming to the controller, the problem is not related to the content, but to the URL used. Without seeing the framework configuration and the complete Ajax code it is impossible to say what is wrong. Look an example and review the request made by the browser (F12 / developer tool) to see if the URL your code generates is the one that Code Igniter expects to receive.

  • 1

    The problem was really in the URL... I hit and here it worked. vlw

Show 4 more comments

Browser other questions tagged

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