【问题标题】:Json unable to pass array to phpJson无法将数组传递给php
【发布时间】:2018-01-18 02:56:10
【问题描述】:

正如标题所说,我未能通过 json ajax 将数据数组传递给 php。我正在使用codeigniter,我做错了什么?这是jQuery代码:

 function load_page_data1(){

    var data = [];
    data['val'] = "solid_t1";
    $.ajax({   

        type: 'POST',  
        url: BASE_URL+'index.php/Chart_varnish/getdata',
        data: data,
        dataType: 'json',               
        success: function(output) {
          alert(output);
      },
      error: function(request, status, error){
        alert("Error: not working");
    }
});

}

这里是php代码:

function getdata(){
$parameter = '';

if(isset($_POST))
{
  if(isset($_POST['val'])) 
  {
  $parameter = $_POST['val'];

} else
{
  echo "failed!";
}
}

$this->load->model('Chart');
$this->load->helper('url');
$data1  = $this->Chart->getdata_solid($parameter);

echo json_encode($data1);

}

决赛:

伙计们,事实证明这些值确实从 jQuery 传递到 php,问题是我在 javascript 函数中愚蠢地调用了两次相同的 php 函数,然后第二次调用时没有发布“val”,所以php函数错误并停止。

谢谢大家的回答,至少我学会了使用jQuery传递数据的不同方法。

【问题讨论】:

  • 你需要对你的数组进行字符串化JSON.stringify(data);
  • 感谢您的回答,按照您的建议尝试后仍然无法正常工作。

标签: php jquery json ajax codeigniter


【解决方案1】:

如果是 not a array 就不要创建 array make a object

var data = {};
data.val = "solid_t1";

// equivalent to 
data ={val:"solid_t1"}; 

// equivalent to 
data['val'] ="solid_t1"; 


$.ajax({   

    type: 'POST',  
    url: BASE_URL+'index.php/Chart_varnish/getdata',
    data: data,
    dataType: 'json',               
    success: function(output) {
      alert(output);
  },
  error: function(request, status, error){
    alert("Error: not working");
}
});

更新1:如果您需要发送数组,您需要创建一个properarray object,如下所示

示例:

var data=[];

item ={val:'value'};

data.push(item);

var  new_data = JSON.stringify(data);
 $.ajax({   

    type: 'POST',  
    url: BASE_URL+'index.php/Chart_varnish/getdata',
    data: new_data,
    dataType: 'json',               
    success: function(output) {
      alert(output);
  },
  error: function(request, status, error){
    alert("Error: not working");
}
});

用于调试:

您的服务器端代码可能有问题。因此,出于测试目的,请在 function 中注释所有行,只需执行此操作 echo json_encode($_POST); 并在您的 ajax 成功函数中添加 console.log(output); 并让我知道结果。

【讨论】:

  • 只是好奇,是不是每次都要JSON.stringify发数据?
  • 没有。如果它是数组,你需要stringify,否则你可以像object这样发送data:{ name:value,name1:value1}
  • 好吧,我只是按照你的建议尝试了,但没有成功。
  • 可能是您的服务器端代码有问题。所以为了测试目的,请在function 中注释所有行,只需执行此操作echo json_encode($_POST); 并在您的 ajax 成功函数中添加console.log(output); 并让我知道结果@Vito
  • 按照你的建议,我在控制台上得到了结果:{val: "solid_t1"},所以这似乎是服务器端代码的问题。 @JYoThI
【解决方案2】:

使用var data = {}; 代替var data = []; 来初始化您的数据。它应该可以解决您的问题。

您的数据没有以 json 格式传递,现在可以了。

代码将是:

        function load_page_data1(){

            var data = {};
            data['val'] = "solid_t1";
            $.ajax({   
                type: 'POST',  
                url: BASE_URL+'index.php/welcome/getdata',
                data: data,
                dataType: 'json',               
                success: function(output) {
                  alert(output);
              },
              error: function(request, status, error){
                alert("Error: not working");
              }
            });

        }

还有一个建议,请使用 CI 输入库在控制器中发布请求。 例如

$parameter = $this->input->post('val');

【讨论】:

    【解决方案3】:

    创建一个json对象

    jsonObj = []; 
    

    根据需要创建数组列表

    item = {}
    item ["val"] = "solid_t1";
    

    将数组列表推入json。

    jsonObj.push(item);
    

    请尝试一下。它在我的情况下工作。

    你的完整代码会像

    function load_page_data1(){
    
        jsonObj = []; 
        item = {}
        item ["val"] = "solid_t1";
        jsonObj.push(item);
    
        $.ajax({   
    
            type: 'POST',  
            url: BASE_URL+'index.php/Chart_varnish/getdata',
            data: jsonObj,
            dataType: 'json',               
            success: function(output) {
              alert(output);
          },
          error: function(request, status, error){
            alert("Error: not working");
        }
    });
    
    }
    

    下面给出了另一种使用方式。

    function load_page_data1(){
    
        var jsonObj = {};
        jsonObj["val"] = "solid_t1"; 
    
        $.ajax({   
    
            type: 'POST',  
            url: BASE_URL+'index.php/Chart_varnish/getdata',
            data: jsonObj,
            dataType: 'json',               
            success: function(output) {
              alert(output);
          },
          error: function(request, status, error){
            alert("Error: not working");
        }
    });
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-02
      • 1970-01-01
      • 2012-01-26
      • 2015-01-12
      • 1970-01-01
      • 2011-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多