我试图使用JQuery函数通过.ajax将2条信息保存到数据库中。我想在postID和userID中保存WordPress。我想我得到了大部分函数的jist,但我需要弄清楚如何将数组发送到处理页面,这样我就可以插入它。这就是我到目前为止所写的:
$(document).ready(function() {
$('#saveme').click(function() {
$.ajax({
type: "POST",
url: "save_data.php",
contentType: "application/json; charset=utf-8",
data: "",
dataType: "json",
success: function (msg) {
alert("This recipe has been saved in your profile area!");
}
});
});有人能透露一些信息到数据值中来存储我可以发送到处理页面的2条信息吗?
我使用的是PHP,代码位于一个.js文件中,所以我可能还需要知道如何将信息发送到js文件。谢谢!!
发布于 2014-02-06 13:16:59
要发送到服务器的数据类型是包含零或多个键值对或字符串的JavaScript对象。为你的案子
data: { 'postID' : $('#postID').val(), 'userID' : $(('#userID').val() },
发布于 2014-02-06 13:04:44
数据应该是包含要保存的数据的JSON对象,即
$(document).ready(function() {
$('#saveme').click(function() {
$.ajax({
type: "POST",
url: "save_data.php",
contentType: "application/json; charset=utf-8",
data: {postID: "A123456", userId: "HGSADKJ"},
dataType: "json",
success: function (msg) {
alert("This recipe has been saved in your profile area!");
}
});
});发布于 2014-02-06 13:06:30
只需创建一个JSON对象并发送它:(假设postID和userID的id在页面上有一个元素
var jsonData = { "postID" : $("#postID").val(),
"userID" : $(("#userID").va;() }
$(document).ready(function() {
$('#saveme').click(function() {
$.ajax({
type: "POST",
url: "save_data.php",
contentType: "application/json; charset=utf-8",
data: jsonData,
dataType: "json",
success: function (msg) {
alert("This recipe has been saved in your profile area!");
}
});
});https://stackoverflow.com/questions/21603770
复制相似问题