AJAX post JSOSN data arrives empty - codeigniter

余生长醉 提交于 2020-01-01 05:00:27

问题


I have found a lot of similar questions, yet no one is related to my question, however, This is my AJAX request

data = JSON.stringify(data);
url = base_url + "index.php/home/make_order";
//alert(url);

var request = $.ajax({
  url: url,
  type: 'POST',
  contentType: 'application/json',
  data: data
});
request.done(function(response){
  alert('success');
});
request.fail(function(jqXHR, textStatus, errorThrown){
  alert('FAILED! ERROR: ' + errorThrown);
});

My problem is that when it arrives to the PHP CI-controller $this->input->post('data') arrives empty!!

UPDATE This is my data: as shown before the AJAX request:

data = {"sum":"2.250","info":[{"id":"6","name":"bla","price":"1.000"}]}

Please help. Thanks in advance.


回答1:


First I'd like to thank all responses. Actually it was a couple of mistakes, First: as @bipen said, data must be sent as an object rather than a string. and when I tried it, it didn't work because I didn't put the single-quote around data

$.ajax({
  url: url,
  type: 'POST',
  contentType: 'application/json',
  data: {'data': data}
});

Second: as @foxmulder said, contentType was misspelled, and should be ContentType so the correct code is:

$.ajax({
  url: url,
  type: 'POST',
  ContentType: 'application/json',
  data: {'data': data}
}).done(function(response){
  alert('success');
}).fail(function(jqXHR, textStatus, errorThrown){
  alert('FAILED! ERROR: ' + errorThrown);
});

and just FYI in case someone had issues with PHP fetching, this is how to do it:

$data = $this->input->post('data');
    $data = json_decode($data);
    $sum = $data->sum;
    $info_obj = $data->info;
    $item_qty = $info_obj[0]->quantity;



回答2:


send your data as object and not string.. (not sure you have done that already unless we see you data's value.. if not then try it)

data = JSON.stringify(data);
url = base_url + "index.php/home/make_order";
        //alert(url);

        var request = $.ajax({
            url         : url,
            type        : 'POST',
            contentType : 'application/json',
            data        : {data:data} //<------here
        });
        request.done(function(response){
            alert('success');
        });
        request.fail(function(jqXHR, textStatus, errorThrown){
            alert('FAILED! ERROR: ' + errorThrown);
        });

updated if as of comments you data is

 {"sum":"2.250","info":[{"id":"6","name":"bla","price":"1.000"}]}

then data:data is fine

 var request = $.ajax({
            url         : url,
            type        : 'POST',
            contentType : 'application/json',
            data        : data
        });

bt you need to change your codeigniter codes to

 $this->input->post('sum') // = 2.250
 $this->input->post('info')



回答3:


contentType should be capitalized (ContentType)

see this question




回答4:


I have extended the CI_Input class to allow using json.

Place this in application/core/MY_input.php and you can use $this->input->post() as usually.

class MY_Input extends CI_Input {

    public function __construct() {
        parent::__construct();
    }

    public function post($index = NULL, $xss_clean = NULL){
        if($xss_clean){
            $json = json_decode($this->security->xss_clean($this->raw_input_stream), true);
        } else {
            $json = json_decode($this->raw_input_stream, true);
        }
        if($json){
            if($index){
                return $json[$index] ?? NULL;
            }
            return $json;
        } else {
            return parent::post($index, $xss_clean);
        }
    }

}

If you are using PHP5.x. Replace

return $json[$index] ?? NULL;

with

return isset($json[$index]) ? $json[$index] : NULL;



回答5:


I'm not familiar with CodeIgniter, but I think you can try with global variable $_POST:

var_dump($_POST['data']);

If var_dump show data, may be $this->input... has problem



来源:https://stackoverflow.com/questions/20285010/ajax-post-jsosn-data-arrives-empty-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!