Php Json_编码数组函数错误
我已根据该假设为不同装箱的装箱产品制作了一个矩阵: 我使用PHP作为服务器端语言,使用jQueryAjax传输数据 前端代码:Php Json_编码数组函数错误,php,jquery,json,ajax,Php,Jquery,Json,Ajax,我已根据该假设为不同装箱的装箱产品制作了一个矩阵: 我使用PHP作为服务器端语言,使用jQueryAjax传输数据 前端代码: 重量箱 板盒 双管箱 四管箱 箱子数量 功能测试(){ var no_of_post=$('no_of_post').val(); $.ajax({ 类型:“POST”, url:'Test/test1', 数据:{no_of_post:no_of_post}, //数据:$('#enqCapture')。序列化(), 成功:功能(resp){ //toastr.su
重量箱
板盒
双管箱
四管箱
箱子数量
功能测试(){
var no_of_post=$('no_of_post').val();
$.ajax({
类型:“POST”,
url:'Test/test1',
数据:{no_of_post:no_of_post},
//数据:$('#enqCapture')。序列化(),
成功:功能(resp){
//toastr.success(“成功-已准备发送详细信息”);
$(“#重量箱”).val(分别为[1]);
$(“#板盒”).val(分别为[3]);
$(“两个管道盒”).val(分别为[5]);
$(“四管箱”).val(分别为[7]);
$(“盒子编号”).val(分别为[9]);
控制台日志(resp);
},
错误:函数(jqXHR,异常){
toastr.error('错误-出错,请重试');
}
});
返回false;
}
我的控制器代码是:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends MY_Controller{
function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->model('Enquiry_model');
$this->load->model('Quotation_model');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->library('session');
$this->load->model('User');
$this->load->model('Main_model');
$this->load->model('Master_model');
$this->is_logged_in();
}
public function index(){
$data['user_data']=$this->is_logged_in();
$this->load->view('admin1/quotation/test',$data);
}
public function test1(){
$no_of_post=$this->input->post('no_of_post');
//$no_of_post."<br>";
$weight_box=0;
$plate_box=0;
$two_pipe_box=0;
$four_pipe_box=0;
if($no_of_post==1){
$weight_box=1;
$two_pipe_box=1;
$total_box=floor($weight_box+$two_pipe_box);
}else{
$weight_box=round($no_of_post/2);
$plate_box=ceil(($no_of_post/5));
$four_pipe_box=$no_of_post/4;
if (strpos($four_pipe_box,'.') !== false) {
$four_pipe_box1=floor($four_pipe_box);
$fraction=$four_pipe_box-$four_pipe_box1;
if($fraction>=.60){
$four_pipe_box=ceil($no_of_post/4);
$total_box=floor($weight_box+$plate_box+$four_pipe_box);
}else{
$four_pipe_box=floor($no_of_post/4);
$two_pipe_box=1;
$total_box=floor($weight_box+$plate_box+$four_pipe_box+$two_pipe_box);
}
}else{
$four_pipe_box=ceil($no_of_post/4);
$total_box=floor($weight_box+$plate_box+$four_pipe_box);
}
}
$arr= array($weight_box,$plate_box,$two_pipe_box,$four_pipe_box,$total_box);
/*$arr[0]=$weight_box;
$arr[1]=$plate_box;
$arr[2]=$two_pipe_box;
$arr[3]=$four_pipe_box;
$arr[4]=$total_box;*/
//$arr[0] = "Mark Reed";
//$arr[1] = "34";
//$arr[2] = "Australia";
//echo json_encode($arr);
echo json_encode(array_values($arr));
//exit();
}
}
?>
但在文本框中返回的结果是错误的。使用:
dataType : 'json',
encode : true
之后:
data: {no_of_post:no_of_post},
因为您正在从控制器返回json编码的数据
data: {no_of_post:no_of_post},