【发布时间】:2014-10-17 15:46:16
【问题描述】:
我正在尝试使用 jquery 从页面中抓取一些元素数据并将其发送到 php 文件以保存在数据库中我正在做的是:
- 我正在创建二维数组并希望将数据传递给 php 文件,以便我可以运行 foreach 加载以保存在数据库中:
错误:现在得到消息:SyntaxError: Unexpected token A in alert box which is alert(errorThrown);
我的 jquery 代码是:
function scrape() {
var info = new Array();
$("div.clip").each(function (index) {
info[index] = {};
info[index]['name'] = $(this).find(".fn").text();
info[index]['rating'] = $(this).find("span.tinyPush").text();
info[index]['review'] = $(this).find("p.description").text();
});
console.log(info);
$.ajax({
type: "POST",
url: "save_scrap.php",
data: {info: info},
dataType: "json",
beforeSend: function () {
// Do something before sending request to server
},
error: function (jqXHR, textStatus, errorThrown) {
alert(errorThrown);
},
success: function (data) {
console.log(data);
$("#div1").html(data);
alert('success!');
}
});
return false;
}
我试图在 php 文件中获取它:
print_r($_POST) ;
如果您认为问题不好或解释不好,请在 cmets 中告诉我。
谢谢。
【问题讨论】:
-
你应该先说什么是行不通的
-
对不起。我无法进入 php 文件,什么都没有显示,所以我可以做任何查询
-
你从
console.log得到什么吗?如果不是,可能是因为var info = new Array();是在.each范围内声明的。将该声明移到.each之前,您应该会很好。 -
移动 var info = new Array(); 后我在控制台中得到了这个[数组[0],数组[0],数组[0],数组[0],数组[0],数组[0],数组[0],数组[0],数组[0],数组[0] , $family: function, $constructor: function, each: function, clone: function, clean: function…]0: Array[0]1: Array[0]2: Array[0]3: Array[0]4:数组[0]5:数组[0]6:数组[0]7:数组[0]8:数组[0]9:数组[0]长度:10__proto__:数组[0]
-
@RyanWillis 我刚刚更新了代码。你能检查一下代码并帮助我吗?
标签: javascript php jquery arrays ajax