jQuery AJAX post JSOSN 数据到达空 - codeigniter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20285010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AJAX post JSOSN data arrives empty - codeigniter
提问by Roshdy
I have found a lot of similar questions, yet no one is related to my question, however, This is my AJAX request
我发现了很多类似的问题,但没有人与我的问题相关,但是,这是我的 AJAX 请求
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!!
我的问题是,当它到达 PHP CI 控制器时$this->input->post('data')到达时为空!
UPDATEThis is my data: as shown before the AJAX request:
更新这是我的数据:如 AJAX 请求之前所示:
data = {"sum":"2.250","info":[{"id":"6","name":"bla","price":"1.000"}]}
数据 = {"sum":"2.250","info":[{"id":"6","name":"bla","price":"1.000"}]}
Please help. Thanks in advance.
请帮忙。提前致谢。
回答by Roshdy
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
首先我要感谢所有的回应。实际上这是几个错误, 首先:正如@bipen 所说,数据必须作为对象而不是字符串发送。当我尝试它时,它不起作用,因为我没有在数据周围放置单引号
$.ajax({
url: url,
type: 'POST',
contentType: 'application/json',
data: {'data': data}
});
Second: as @foxmulder said, contentTypewas misspelled, and should be ContentTypeso the correct code is:
第二:正如@foxmulder 所说,contentType拼写错误,应该是ContentType,所以正确的代码是:
$.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:
仅供参考,以防有人在获取 PHP 时遇到问题,这是如何做到的:
$data = $this->input->post('data');
$data = json_decode($data);
$sum = $data->sum;
$info_obj = $data->info;
$item_qty = $info_obj[0]->quantity;
回答by bipen
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);
});
updatedif as of comments you data is
如果您的数据在评论时更新
{"sum":"2.250","info":[{"id":"6","name":"bla","price":"1.000"}]}
then data:data
is fine
然后data:data
很好
var request = $.ajax({
url : url,
type : 'POST',
contentType : 'application/json',
data : data
});
bt you need to change your codeigniter codes to
bt 您需要将您的 codeigniter 代码更改为
$this->input->post('sum') // = 2.250
$this->input->post('info')
回答by foxmulder
回答by Firze
I have extended the CI_Inputclass to allow using json.
我扩展了CI_Input类以允许使用 json。
Place this in application/core/MY_input.phpand you can use $this->input->post()as usually.
把它放在application/core/MY_input.php 中,你可以像往常一样使用$this->input->post()。
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
如果您使用的是PHP5.x。代替
return $json[$index] ?? NULL;
with
和
return isset($json[$index]) ? $json[$index] : NULL;
回答by Davuz
I'm not familiar with CodeIgniter, but I think you can try with global variable $_POST
:
我不熟悉 CodeIgniter,但我认为您可以尝试使用全局变量$_POST
:
var_dump($_POST['data']);
If var_dump show data, may be $this->input...
has problem
如果var_dump显示数据,可能$this->input...
有问题