Javascript 通过JQuery.ajax将数据保存到数据库
我试图使用.ajax函数通过JQuery将两条信息保存到数据库中。我想在WordPress中保存postID和userID。我想我已经获得了大部分函数的jist,但需要弄清楚如何将数组发送到处理页面,以便插入它。这是我到目前为止写的:Javascript 通过JQuery.ajax将数据保存到数据库,javascript,php,jquery,ajax,wordpress,Javascript,Php,Jquery,Ajax,Wordpress,我试图使用.ajax函数通过JQuery将两条信息保存到数据库中。我想在WordPress中保存postID和userID。我想我已经获得了大部分函数的jist,但需要弄清楚如何将数组发送到处理页面,以便插入它。这是我到目前为止写的: $(document).ready(function() { $('#saveme').click(function() { $.ajax({ type: "POST", u
$(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文件。谢谢 数据应该是包含要保存的数据的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!");
}
});
});
只需创建一个JSON对象并发送: (假设页面上有一个由postID和userID组成的元素
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!");
}
});
});
你忘记添加数据了
$(document).ready(function() {
var postID='<?php the_id(); ?>';
var userId='<?php author_id(); ?>';
$('#saveme').click(function() {
$.ajax({
type: "post",
url: "save_data.php",
contentType: "application/json; charset=utf-8",
data: {postID: postID, userId: userId},
dataType: "json",
success: function (msg) {
alert("This recipe has been saved in your profile area!");
}
});
});
$(文档).ready(函数(){
var postID='';
var userId='';
$('#saveme')。单击(函数(){
$.ajax({
类型:“post”,
url:“save_data.php”,
contentType:“应用程序/json;字符集=utf-8”,
数据:{postID:postID,userId:userId},
数据类型:“json”,
成功:功能(msg){
警告(“此配方已保存在您的配置文件区域!”);
}
});
});
要发送到服务器的数据类型是包含零个或多个键值对或字符串的JavaScript对象
数据:{
'postID':$('#postID').val(),
“userID”:$(“#userID”).val()
},
数据:,
为空数据
是ajax
对象的键,在该对象中将数据发送到save_data.php
。data关键字存储将在其他页面上通过ajax请求发送的值。您必须在var id='2'中定义您的值,并将其存储在data:“id=”id;没有类似于“JSON对象”。您有表示对象的JSON,但它是一个字符串。您在该代码中设置的data
属性值是一个JavaScript对象。JSON无论如何都不是字符串。它是一个对象:typeof{foo:'bar'}==”对象"
不,这是不正确的。JSON是JavaScript对象表示法;它是对象或数组的字符串表示法。您刚才描述的是JavaScript对象,而不是JSON。看看第一个示例,它使用了一个名为myJSONObject的变量。但我不知道,也许道格拉斯·克罗克福德不知道如何命名他的变量……很明显,您是对的他没有。术语“JSON对象”是胡说八道。他们没有忘记添加它,整个问题是“它应该是什么?”没有“JSON对象”这样的东西。JavaScript有对象,JSON是一个可能代表一个的字符串。谢谢!我需要为页面上的postID和userID添加隐藏输入,这样我才能获得这些吗?