0

嘿伙计们,我阅读了其他一些帖子并尝试了很多,但它仍然不适合我。当我提醒数组时,我在第一个站点上得到了所有结果,但是在将数据发送到 php 之后,我得到了一个空结果。有任何想法吗?

$(document).ready(function() {
$('#Btn').click(function() {
    var cats = [];
    $('#cats input:checked').each(function() {
        cats.push(this.value);
    });

    var st = JSON.stringify(cats);
   $.post('foo.php',{data:st},function(data){cats : cats});
   window.location = "foo.php";     
   }); 
});

php

$data = json_decode($_POST['data']);

谢谢你

当我提醒它房屋/公寓、花园/自然、运动/爱好时,我的阵列看起来像这样,这是用户可能选择的几个结果(从复选框中)。但是当我将它发布到 php 时,我什么也得不到。当我使用请求标记(chrome扩展)时,它向我显示了类似原始数据猫=%5B%22house+themes%22%2C%22flat+items%22%5D

我也尝试过这种方式 - 仍然没有结果

$(document).ready(function() {
$('#Btn').click(function() {
    var cats = [];
    $('#cats input:checked').each(function() {
        cats.push(this.value);
        alert(cats);

    $.ajax({

    type: 'POST',
    url: "foo.php",
    data: {cats:  JSON.stringify(cats)},
    success: function(data){
     alert(data);
     }
   });
  }); 
   window.location = "foo.php";
  }); 
}); 

php:

$json = $_POST['cats'];
$json_string = stripslashes($json);
$data = json_decode($json_string, true);
echo "<pre>";
print_r($data);

它让我发疯

4

4 回答 4

1

拿这个脚本:https ://github.com/douglascrockford/JSON-js/blob/master/json2.js

并调用:

var myJsonString = JSON.stringify(yourArray);

所以现在你的代码是

$(document).ready(function() {
$('#Btn').click(function() {
    var cats = [];
    $('#cats input:checked').each(function() {
        cats.push(this.value);
    });
   var st = JSON.stringify(cats);
   $.post('foo.php',{data:st},function(data){cats : cats});
 //  window.location = "foo.php";    // comment this by this page redirect to this foo.php
   });
   }); 

//如果你想重定向然后使用下面的代码

-------------------------------------------------
     $.post('foo.php',{data:st},function(data){
       window.location = "foo.php"; 
     });
---------------------------------------------------

php

$data = json_decode($_POST['data']);
于 2013-11-13T08:58:58.337 回答
1
var ItemGroupMappingData = []

Or 

var ItemGroupMappingData =
{
"id" : 1,
"name" : "harsh jhaveri",
"email" : "test@test.com"
}

 $.ajax({
            url: 'url link',
            type: 'POST',
            dataType: "json",
            data: ItemGroupMappingData,
            success: function (e) {
// When server send response then it will be comes in as object in e. you can find data //with e.field name or table name
            },
            error: function (response) {
                //alert(' error come here ' + response);
                ExceptionHandler(response);
            }
        });
于 2013-11-13T09:05:06.443 回答
0

尝试这个 :-

    $data = json_decode($_POST['data'], TRUE);
于 2013-11-13T08:57:29.720 回答
0

我认为您应该将“window.location =”移动到帖子回调中,这意味着它应该等到帖子完成然后重定向页面。

$.post('foo.php', {
  data : st
}, function(data) {

   window.location = "foo.php";
});
于 2013-11-13T09:01:44.853 回答