Php iam返回json_encode格式的数据,并尝试用ajax记录输出,但出现解析错误。?
我的模型--我从Mysql数据库获取数据并将其返回给控制器Php iam返回json_encode格式的数据,并尝试用ajax记录输出,但出现解析错误。?,php,jquery,mysql,json,ajax,Php,Jquery,Mysql,Json,Ajax,我的模型--我从Mysql数据库获取数据并将其返回给控制器 public function getCharges(){ $where = "charge_type = 'variable'; $data = $this->read('charges',$where); return $data; } 我的控制器——这里我将json格式的数据编码为ajax if (isset($_POST['method']) && $_POST['metho
public function getCharges(){
$where = "charge_type = 'variable';
$data = $this->read('charges',$where);
return $data;
}
我的控制器——这里我将json格式的数据编码为ajax
if (isset($_POST['method']) && $_POST['method'] == 'getCharges'){
$this->load->model('Apiweb_m');
$data = $this->Apiweb_m->getCharges();
echo json_encode($data);
}
我的阿贾克斯
var method = "getCharges";
$.ajax({
type: 'POST',
data: 'method='+method,
url : api_url+"apiweb",
/* dataType: "json",*/
success:function(msg){
var data = $.parseJSON(msg);
console.log(data);// here iam getting Parse error
},
error:function(Xhr, status, error){
console.log(Xhr);
console.log(status);
console.log(error);
}
将模型中的功能更改为:
public function getCharges(){
$where = "charge_type = 'variable'";
$data = $this->read('charges',$where);
return $data;
}
和ajax:
var method = "getCharges";
$.ajax({
type: 'POST',
data: {method:method},
url : api_url+"apiweb",
dataType: "json",
success:function(msg){
var data = $.parseJSON(msg);
console.log(data);// here iam getting Parse error
},
error:function(Xhr, status, error){
console.log(Xhr);
console.log(status);
console.log(error);
}
您必须设置数据类型ajax属性json。希望您能这样做
var method = "getCharges";
$.ajax({
type: 'POST',
data: 'method='+method,
url : api_url+"apiweb",
dataType: "json",
success:function(msg){
var data = $.parseJSON(msg);
console.log(data);// here iam getting Parse error
},
error:function(Xhr, status, error){
console.log(Xhr);
console.log(status);
console.log(error);
}
请添加您得到的示例ajax响应,另外,如果它的响应正确,则
contentType:'application/json'
add-in请求