AJAX发布JSOSN数据为空-Codeigniter [英] AJAX post JSOSN data arrives empty - codeigniter

查看:136
本文介绍了AJAX发布JSOSN数据为空-Codeigniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了很多类似的问题,但是没有人与我的问题相关,但这是我的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请求之前所示:

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

data = {"sum":"2.250","info":[{"id":"6","name":"bla","price":"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 所以正确的代码是:

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;

这篇关于AJAX发布JSOSN数据为空-Codeigniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆