Javascript 使用JSON将数组从Jquery传递到PHP
嘿,伙计们,我读了一些其他的帖子,尝试了很多,但它仍然不适合我。 当我通知数组时,我在第一个站点上得到所有结果,但在将数据发送到php之后,我只得到一个空结果。有什么想法吗Javascript 使用JSON将数组从Jquery传递到PHP,javascript,php,jquery,arrays,json,Javascript,Php,Jquery,Arrays,Json,嘿,伙计们,我读了一些其他的帖子,尝试了很多,但它仍然不适合我。 当我通知数组时,我在第一个站点上得到所有结果,但在将数据发送到php之后,我只得到一个空结果。有什么想法吗 $(document).ready(function() { $('#Btn').click(function() { var cats = []; $('#cats input:checked').each(function() { cats.push(this.value); })
$(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
谢谢你
当我提醒它房子/公寓、花园/自然、运动/爱好时,我的阵列看起来像这样
这是用户可以选择的几个结果(从复选框中)。
但是当我把它发布到php时,我什么也得不到。当我使用请求标记(chrome扩展)时,它会显示一些类似原始数据cats=%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=$\u POST['cats'];
$json_string=stripslashes($json);
$data=json\u decode($json\u字符串,true);
回声“;
打印(数据);
它让我疯狂试试这个:-
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";
});
---------------------------------------------------
//如果你想直接使用,请使用下面的代码
$data = json_decode($_POST['data']);
Php
我认为您应该将“window.location=”移动到post回调,这意味着它应该等到post找到后再重定向页面
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);
}
});
删除window.location=“foo.php”&然后尝试感谢您的帮助,但这不起作用。不幸的是,url:“url链接”在这里,您可以将url作为abc.aspx/savedata或您的页面链接和方法名传递。谢谢,但它仍然不起作用。chrome扩展向我显示此数据已提交数据=%5B%22其他+主题%22%2C%22经济%2F政治%22%2C%22儿童%2F+婴儿%2F家庭%22%2C%22房子%2F公寓%2F花园%22%5D但我无法回应任何内容,而使用变量转储我只得到空值
-------------------------------------------------
$.post('foo.php',{data:st},function(data){
window.location = "foo.php";
});
---------------------------------------------------
$data = json_decode($_POST['data']);
$.post('foo.php', {
data : st
}, function(data) {
window.location = "foo.php";
});
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);
}
});