admin管理员组文章数量:1406308
I'm helping a family member out with a WordPress site and haven't coded in PHP in some time. I need to change the way they are handling http requests and have read the following documentation:
<?php
/**
* Plugin Name: First Plugin
* Plugin URI: localhost
* Description: First Plugin
* Version: 1.0
* Author: Giltea
* Author URI: webmeau.ca
**/
global $wpdb;
$wpdb->show_errors();
add_action( 'wp_enqueue_scripts', 'my_enqueue' );
function my_enqueue() {
wp_enqueue_script( 'ajax-script',plugins_url('firstJquery.js', __FILE__), array('jquery') );
wp_localize_script( 'ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action('wp_ajax_getListings', 'getListings');
add_action('wp_ajax_nopriv_getListings', 'getListings');
function getListings(){
global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM wp_wpbdp_listings", OBJECT );
echo $results;
wp_die();
}
?>
I then have a small Jquery script in a separate file:
// firstJQuery.js
(function($) {
$.ajax({
url: my_ajax_object.ajax_url,
type: 'GET',
dataType: 'json', // added data type
action: 'getListings',
success: function(res) {
console.log(res);
}
});
}(jQuery))
I am getting a 400 bad request when it's hitting the admin-ajax.php endpoint. I am getting no other errors in any of my logs (apache, php, or WordPress) and have turned on debugging in the wp-config.php file. Anyone have any suggestions?
I'm helping a family member out with a WordPress site and haven't coded in PHP in some time. I need to change the way they are handling http requests and have read the following documentation:
https://codex.wordpress/AJAX_in_Plugins
<?php
/**
* Plugin Name: First Plugin
* Plugin URI: localhost
* Description: First Plugin
* Version: 1.0
* Author: Giltea
* Author URI: webmeau.ca
**/
global $wpdb;
$wpdb->show_errors();
add_action( 'wp_enqueue_scripts', 'my_enqueue' );
function my_enqueue() {
wp_enqueue_script( 'ajax-script',plugins_url('firstJquery.js', __FILE__), array('jquery') );
wp_localize_script( 'ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action('wp_ajax_getListings', 'getListings');
add_action('wp_ajax_nopriv_getListings', 'getListings');
function getListings(){
global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM wp_wpbdp_listings", OBJECT );
echo $results;
wp_die();
}
?>
I then have a small Jquery script in a separate file:
// firstJQuery.js
(function($) {
$.ajax({
url: my_ajax_object.ajax_url,
type: 'GET',
dataType: 'json', // added data type
action: 'getListings',
success: function(res) {
console.log(res);
}
});
}(jQuery))
I am getting a 400 bad request when it's hitting the admin-ajax.php endpoint. I am getting no other errors in any of my logs (apache, php, or WordPress) and have turned on debugging in the wp-config.php file. Anyone have any suggestions?
Share Improve this question asked Nov 19, 2019 at 6:59 Husk RekomsHusk Rekoms 1033 bronze badges1 Answer
Reset to default 1The PHP part of your code looks more or less OK. (There are some security concerns and you shouldn’t echo an array of objects, but you’ll get it when you see it).
But your code doesn’t work because of an error in your JS.
You should send “action” as data and not as a setting... so it should look like this:
$.ajax({
url: my_ajax_object.ajax_url,
type: 'GET',
dataType: 'json', // added data type
data: {
action: 'getListings',
},
success: function(res) {
console.log(res);
}
});
本文标签: pluginsProper way to handle adminajax calls
版权声明:本文标题:plugins - Proper way to handle admin-ajax calls 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744991511a2636415.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论