Php jQuery AJAX与wordpress数据库连接
我试图通过单击按钮来调用jqueryajax。 首先,我开始创建自己的AJAX代码,除了IOS之外,一切都正常工作 所以现在我开始使用jQuery。来回发送数据就像一个魔咒, 例外。。每次我尝试连接php文件上的数据库时,都会出现错误500(内部服务器错误)。 我试过wordpress way($wpdb)和mysqli,但都不起作用 Javascript:Php jQuery AJAX与wordpress数据库连接,php,jquery,ajax,Php,Jquery,Ajax,我试图通过单击按钮来调用jqueryajax。 首先,我开始创建自己的AJAX代码,除了IOS之外,一切都正常工作 所以现在我开始使用jQuery。来回发送数据就像一个魔咒, 例外。。每次我尝试连接php文件上的数据库时,都会出现错误500(内部服务器错误)。 我试过wordpress way($wpdb)和mysqli,但都不起作用 Javascript: jQuery(document).ready(function() { var buttons = document.getElemen
jQuery(document).ready(function() {
var buttons = document.getElementsByClassName('vilbutton');
for(var i=0; i< buttons.length; i++){
jQuery(buttons[i]).one('click',send);
}
function send(){
var id = this.id;
var count = 1;
jQuery.ajax({
url: 'mydomain/krant.php',
type: 'post',
dataType: 'text',
data: { id : id, count : count},
crossDomain: true,
success: function(data) {
jQuery("#"+ id).children('.vilbsmall').children('.vilbsmallc').html("<span class='vilbsmallct' style='height: 30px; width: 30px;'>" + data + "</span>");
},
error: function(xhr, desc, err) {
alert("Foutcode: 1912");
console.log(xhr);
console.log("Details: " + desc + "\nError:" + err);
}
}); // end ajax call
}
});
这件事我坚持了两天。。非常感谢您的帮助 通过包含wp-load.php,您可以访问$wpdb之类的对象
require_once( '/path/to/your/files/wp-load.php' );
出现500内部错误的原因是PHP脚本中有错误。
您正在使用未定义的
$wpdb
对象。要访问$wpdb
,请包括wp load.php
require_once( 'path-to-file/wp-load.php' );
我看到您有一个名为
krant.php
的自定义文件,您正在尝试使用$wpdb
对象。这是主题的一部分吗?模板?插件?或者您只是创建了一个php文件,并假设您可以使用全局$wpdb
对象?这是我主题的一部分,它变得更奇怪,就像我说的,我已经让它在同一目录下的另一个文件上运行include数据库连接。此文件可以使用$wpdb成功连接。非常感谢!作品我不知道为什么它在没有require@user3708994因为如果你直接访问wordpress文件,你不能使用任何wordpress函数,除非你加载wp-load.php
require_once( 'path-to-file/wp-load.php' );