[英]Nothing's in $_POST. Is there something wrong with my code?
I am fairly new to JavaScript and PHP, and it's the first time to encounter and use JSON. 我对JavaScript和PHP相当陌生,这是第一次遇到和使用JSON。 I think my AJAX request is fine, but even so, the
$_POST
array is empty. 我认为我的AJAX请求很好,但是即使这样,
$_POST
数组还是空的。
Here is the AJAX call: 这是AJAX调用:
$( "#submit" ).click( function() {
var submit_basic = $.post( 'index.php/submit_data/pass_basic_data',
get_experience_data()
);
submit_basic.done( function(data) {
alert(data); // for debugging purposes
});
});
And this is the function that takes the data from a table: 这是从表中获取数据的函数:
function get_experience_data() {
var temp, data_exp = [];
$( "#exptable tbody tr" ).each( function() {
temp = {
company: $(this).find('td').eq(0).html(),
position: $(this).find('td').eq(1).html(),
datestart: $(this).find('td').eq(2).html(),
dateend: $(this).find('td').eq(3).html(),
description: $(this).find('td').eq(4).html()
};
data_exp = data_exp.concat(temp);
});
return data_exp;
}
And for reference, the destination controller function that only prints the $_POST
array (by the way I am using CodeIgniter): 作为参考,目标控制器函数仅打印
$_POST
数组(通过我使用CodeIgniter的方式):
public function pass_basic_data() {
var_dump($_POST);
}
Can you please pinpoint the error I've made, since I can't find it. 您能指出我犯的错误吗,因为我找不到它。 Thanks a lot!
非常感谢!
UPDATE: I am getting this message in particular: 更新:我特别收到此消息:
array(1) {
["undefined"] =>
string(0) ""
}
UPDATE: 更新:
Thanks for all the help guys! 感谢所有的帮助! I already solved it.
我已经解决了 It made me dance all around the room.
它使我到处跳舞。 I wrapped the return value in a
{name : value}
pair. 我将返回值包装在
{name : value}
对中。
$( "#submit" ).click( function() {
var post_vars = get_experience_data();
var submit_basic = $.post( 'index.php/submit_data/pass_basic_data',
{object: post_vars}
);
submit_basic.done( function(data) {
alert(data); // for debugging purposes
});
});
我建议从以下步骤开始:
var submit_basic = $.post('index.php/submit_data/pass_basic_data', get_experience_data());
Try this: 尝试这个:
var submit_basic = $.post('index.php/submit_data/pass_basic_data', get_experience_data());
// you need to add parentheses here --------------------------------------------------^
When you pass a function to $.post()
it assumes it is a callback that is to be called after the response is received. 当您将函数传递给
$.post()
它将假定它是一个在收到响应后将被调用的回调。 What you want to do is call the function and pass it's return value in to $.post()
. 您要做的就是调用该函数并将其返回值传递给
$.post()
。
By the way, this line: 顺便说一句,这行:
data_exp = data_exp.concat(temp);
could be replaced with: 可以替换为:
data_exp.push(temp);
No need to be creating a new array every time if you're just adding a value to the end. 如果只是在最后添加一个值,则不必每次都创建一个新数组。
您需要执行方法get_experience_data,否则您将传递不执行它的函数
try troubleshooting the actual jquery, not the php part. 尝试对实际的jquery(而不是php部分)进行故障排除。 but heres a suggestion:
但是这是一个建议:
var post_vars = get_experience_data();
var submit_basic = $.post( 'index.php/submit_data/pass_basic_data', post_vars );
here is a conclusion to my previous answer plus the comments, but im still not sure if you could stingify arrays in arrays.. 这是我以前的答案加上注释的结论,但是我仍然不确定是否可以将数组中的数组隐式化。
var submit_basic = $.ajax({
type: "POST",
url: 'index.php/submit_data/pass_basic_data',
dataType: 'json',
async: false,
data: JSON.stringify(get_experience_data()),
success: function (response) {
alert(response);
}
});
UPDATE: modify your get_experience_data function according to this jsfiddle script 更新:根据此jsfiddle脚本修改get_experience_data函数
like this: 像这样:
temp = '{ ';
temp+= '"company":"' +$(this).find('td').eq(0).html()+ '", ';
temp+= '"position":"' +$(this).find('td').eq(1).html()+ '", ';
temp+= '"datestart":"' +$(this).find('td').eq(2).html()+ '", ';
temp+= '"dateend":"' +$(this).find('td').eq(3).html()+ '", ';
temp+= '"description":"' +$(this).find('td').eq(4).html()+ '"';
temp+= ' }';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.