问题描述
我发现了很多类似的问题,但是没有人与我的问题相关,但这是我的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控制器 $ this-> input-> post('data')时,到达 empty !!
My problem is that when it arrives to the PHP CI-controller $this->input->post('data') arrives empty!!
更新这是我的数据:如AJAX请求之前所示:
UPDATEThis is my data: as shown before the AJAX request:
请帮助.预先感谢.
推荐答案
首先,我要感谢所有答复.实际上这是两个错误,第一:正如@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 所以正确的代码是:
Second: as @foxmulder said, contentType was misspelled, and should be ContentTypeso 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;
这篇关于AJAX发布JSOSN数据为空-Codeigniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!