du31992
2016-10-19 03:06
采纳率: 0%
浏览 85

无法从jquery访问数组

Okay prior to my previous question I have tried a bit of modification to my code but somehow I am still lagging.

This is jquery I have created

$(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);
               } 
            });
        });
    });

And addsale.php

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

Can anyone tell me what is missing to access the array and get the html from addsale.php to current page?

图片转代码服务由CSDN问答提供 功能建议

好的,在我之前的问题之前,我已经对我的代码进行了一些修改,但不知怎的,我仍然落后。< / p>

这是我创建的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,\  n dataType:“json”,
 success:function()
 {
 console.log(data.reply); 
 alert(data.reply); 
} 
}); 
});  
  }}; 
   
 
 

并且addsale.php

  if(isset($ _ POST ['push'])  )//也尝试了它的注释!
 {
 $ data = array(); 
 $ data = json_decode(stripslashes($ _ POST ['data'])); 
 
 
 
 
($ data as $ d){\  n echo $ d; 
} 
} 
   
 
 

任何人都可以告诉我访问数组时缺少什么,并从addsale.php获取html到当前页面 ?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • doucongqian6644 2016-10-19 03:13
    已采纳
    $(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);
               } 
            });
        });
    });
    

    in your PHP code try

    if(isset($_POST['data'])) //tried it commenting also!
    {
       $data = array();
       $data = json_decode(stripslashes($_POST['data']));
       foreach($data as $d){
           echo $d;
       }
     }
    
    点赞 打赏 评论
  • dongpeng8994 2016-10-19 03:28

    echo with foreach will not good handling for json response ! You should loop that json after response to ajax success function ...

    addsale.php

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

    access json resposne in ajax

    $.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);
               } 
            });
    
    点赞 打赏 评论
  • dousui3124 2016-10-19 04:26

    i think you have everything correct but missing something right data: {data: ... }, you should also add something like below-

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

    and in php code you try to decode the an array which is not in json format, your code just need to be like this-

      if(isset($_POST['push'])) //tried it commenting also!
      {
          foreach($_POST['data'] as $d){
             echo $d;
          }
      }
    
    点赞 打赏 评论
  • dongya9904 2016-10-19 06:28

    No data argument in your success function. Modify your 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();
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题