阿贾克斯后JSON数据到达空 - codeigniter数据、阿贾克斯后、JSON、codeigniter

2023-09-11 22:34:19 作者:掌中花.

我发现了很多类似的问题,但没有一个是与我的问题,但是,这是我的Ajax请求

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);
            });

我的问题是,当它到达了PHP CI-控制器的 $这个 - >输入 - >后('数据')到达空 !!

更新 这是我的数据:作为Ajax请求之前所示:

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

数据= {总和:2.250,信息:[{ID:6,名:喇嘛,价格:1.000}]}

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

请帮忙。先谢谢了。

推荐答案

首先,我要感谢所有的响应。 其实这是一对夫妇的错误, 第一:作为@bipen说,数据必须被作为一个对象而不是一个字符串。当我试了一下,没有工作,因为我没有把在围绕数据单引号

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}
            });

二:作为@foxmulder说,的contentType 拼写错误,而应该是的ContentType 所以正确的code是:

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);
            });

和仅供参考,以防有人曾用PHP取的问题,这是如何做到这一点:

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;