【问题标题】:Cannot access array from jquery无法从 jquery 访问数组
【发布时间】:2016-10-19 03:06:22
【问题描述】:

好的,在我上一个问题之前,我已经尝试对我的代码进行一些修改,但不知何故我仍然落后。

这是我创建的 jquery

$(document).ready(function()
    {
        var array_ids = [];
        $('.add').click(function()
        {
            array_ids.push($(this).parent().siblings('.row_id').html().trim());
            alert(array_ids);    
        });

        $('.show').click(function(e)
        {
            //e.preventDefault();
            var jsonString = JSON.stringify(array_ids);
            $.ajax(
            {
               method: 'POST',
               url: 'addsale.php',
               data: {data : jsonString},
               cache: false,
               dataType: "json",
               success: function()
               {
                 console.log(data.reply);
                alert(data.reply);
               } 
            });
        });
    });

还有addsale.php

if(isset($_POST['push'])) //tried it commenting also!
{
$data = array();
$data = json_decode(stripslashes($_POST['data']));
foreach($data as $d){
 echo $d;
 }
 }

谁能告诉我访问数组和从addsale.php获取html到当前页面缺少什么?

【问题讨论】:

  • 使用$_POST['data']
  • $data = $_POST['data'];这东西??
  • 您的$.ajax() 成功回调使用了一个未定义的变量data(您可能希望它声明为success: function(data) { ...)。

标签: javascript php jquery


【解决方案1】:
$(document).ready(function()
{
    var array_ids = [];
    $('.add').click(function()
    {
        array_ids.push($(this).parent().siblings('.row_id').html().trim());
        alert(array_ids);    
    });

    $('.show').click(function(e)
    {
        //e.preventDefault();
        //prefer parse function
        var jsonString = JSON.stringify(array_ids);
        $.ajax(
        {
           method: 'POST',
           url: 'addsale.php',
           data: {"data" : jsonString},
           cache: false,
           dataType: "json",
           //e is the response text from your PHP code
           success: function(e)
           {
             //I don't know why this code
             //console.log(data.reply);
            //alert(data.reply);
           } 
        });
    });
});

在你的 PHP 代码中尝试

if(isset($_POST['data'])) //tried it commenting also!
{
   $data = array();
   $data = json_decode(stripslashes($_POST['data']));
   foreach($data as $d){
       echo $d;
   }
 }

【讨论】:

  • JSON.parse:JSON 数据的第 1 行第 3 列的 JSON 数据后出现意外的非空白字符
  • 每当我点击添加时,它都会像这样保存 1 然后再次点击 1,2 然后再次点击 1,2,3
  • 数组到 Json 没关系 stringify()。如果你想要 Json {"key":"value"},你可以使用 parse()。最好使用 json 进行从 PHP 到 Javascript 的通信,反之亦然。
【解决方案2】:

使用foreach 回显不会很好地处理 json 响应!您应该在响应 ajax success 函数后循环该 json ...

addsale.php

if(isset($_POST['data'])) //tried it commenting also!
{
   $data = array();
   $data = json_decode(stripslashes($_POST['data']));
   echo json_encode($data);
 }

在ajax中访问json响应

$.ajax(
        {
           method: 'POST',
           url: 'addsale.php',
           data: {"data" : jsonString},
           cache: false,
           dataType: "json",
           success: function(d)
           {
             //I don't know why this code
             console.log(d);
           } 
        });

【讨论】:

    【解决方案3】:

    我认为你的一切都是正确的,但缺少正确的东西data: {data: ... },你还应该添加如下内容-

     data : {data : jsonString, 'push':'push'},
    

    在 php 代码中,您尝试解码不是 json 格式的数组, 你的代码只需要像这样-

      if(isset($_POST['push'])) //tried it commenting also!
      {
          foreach($_POST['data'] as $d){
             echo $d;
          }
      }
    

    【讨论】:

      【解决方案4】:

      成功函数中没有 data 参数。修改你的addsale.php

      $(document).ready(function()
      {
          var array_ids = [];
          $('.add').click(function()
          {
              array_ids.push($(this).parent().siblings('.row_id').html().trim());
              alert(array_ids);    
          });
          $('.show').click(function(e)
          {
              //e.preventDefault();
              var jsonString = JSON.stringify(array_ids);
              $.ajax(
              {
                 method: 'POST',
                 url: 'addsale.php',
                 data: {data : jsonString},
                 cache: false,
                 dataType: "json",
                 success:function(data)
                 {
                   console.log(data.reply);
                   alert(data.reply);
                 } 
              });
          });
      });
      

      addsale.php

      <?php
      if(isset($_POST['data'])) //tried it commenting also!
      {
      $data=json_decode(stripslashes($_POST['data']));
      echo json_encode(['reply'=>$data]);
      exit();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-28
        • 1970-01-01
        • 2018-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多