我需要在wordpress管理插件中使用AJAX请求WPDB
仅执行时,方法$ Cidade-> inserirCidade可以正常工作,但是当用AJAX调用该方法时,在控制台中给我错误-> POST http://wordpress.loc/wp-admin/admin-ajax.php 500(内部服务器错误)
private function __construct() {
add_action( 'wp_ajax_my_action', array( $this, 'my_action' ));
add_action( 'admin_footer', array ( $this, 'my_action_javascript' ));
}
public static function my_action_javascript() { ?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
var data = {
'action': 'my_action',
'codigo': 222
};
jQuery.post(ajaxurl, data, function(response) {
alert('Got this from the server: ' + response);
});
});
</script> <?php
}
public function my_action() {
$codigo = intval( $_POST['codigo'] );
$nome = 'Meu Nome';
$ibge = 123;
$uf = 'DF';
$Cidade = new Cidade;
$result = $Cidade->inserirCidade($codigo, $nome, $ibge, $uf);
wp_die(); // this is required to terminate immediately and return a proper response
}
我在控制台中收到错误-> POST http://wordpress.loc/wp-admin/admin-ajax.php 500(内部服务器错误)