admin管理员组文章数量:1277377
I'm trying to send ajax requests from a React.js app to wordpress's admin-ajax.php. Here is a mini test plugin I wrote to test this:
// plugins folder > xhrLink/xhrLink.php
<?php
/*
Plugin Name: Xhr Link
*/
function basicPage() {
echo "<h1>Admin Page</h1>";
}
function setupParams() {
add_options_page('XHR Link', 'XHR Link', 'manage_options', 'xhr-link', 'basicPage');
}
add_action('admin_menu', 'setupParams');
function handleXhrLink() {
wp_send_json('this response came from the back end');
}
add_action('wp_ajax_xhrLink', 'handleXhrLink');
// theme folder > functions.php
function add_cors_http_header() {
header("Access-Control-Allow-Origin: *");
}
add_action('init','add_cors_http_header');
In the React.js app I have the following function set to fire on the onClick attribute of a button:
const sendXhrLinkRequest = () => {
let form_data = new FormData;
form_data.append('action', 'xhrLink');
axios
.post('http://localhost/wptest2/wp-admin/admin-ajax.php', form_data)
.then(response => {
console.log(response, `=====sendXhrLinkRequest response=====`);
});
};
From WordPress AJAX with Axios:
FormData interface provides a way to easily construct a set of key/value pairs representing form fields and their values, which can then be easily sent using the XMLHttpRequest.send() method. It uses the same format a form would use if the encoding type were set to "multipart/form-data".
After clicking the button the Chrome console shows this:
VM596:1 POST http://localhost/wptest2/wp-admin/admin-ajax.php 400 (Bad Request)
(anonymous) @ VM596:1
VM596:1 XHR failed loading: POST "http://localhost/wptest2/wp-admin/admin-ajax.php".
createError.js:16 Uncaught (in promise) Error: Request failed with status code 400
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.handleLoad (xhr.js:59)
Does anyone know what causes these errors and how to handle them?
Also tried:
- Submitting the form data as a JSON object. Same console entries resulted.
I'm trying to send ajax requests from a React.js app to wordpress's admin-ajax.php. Here is a mini test plugin I wrote to test this:
// plugins folder > xhrLink/xhrLink.php
<?php
/*
Plugin Name: Xhr Link
*/
function basicPage() {
echo "<h1>Admin Page</h1>";
}
function setupParams() {
add_options_page('XHR Link', 'XHR Link', 'manage_options', 'xhr-link', 'basicPage');
}
add_action('admin_menu', 'setupParams');
function handleXhrLink() {
wp_send_json('this response came from the back end');
}
add_action('wp_ajax_xhrLink', 'handleXhrLink');
// theme folder > functions.php
function add_cors_http_header() {
header("Access-Control-Allow-Origin: *");
}
add_action('init','add_cors_http_header');
In the React.js app I have the following function set to fire on the onClick attribute of a button:
const sendXhrLinkRequest = () => {
let form_data = new FormData;
form_data.append('action', 'xhrLink');
axios
.post('http://localhost/wptest2/wp-admin/admin-ajax.php', form_data)
.then(response => {
console.log(response, `=====sendXhrLinkRequest response=====`);
});
};
From WordPress AJAX with Axios:
FormData interface provides a way to easily construct a set of key/value pairs representing form fields and their values, which can then be easily sent using the XMLHttpRequest.send() method. It uses the same format a form would use if the encoding type were set to "multipart/form-data".
After clicking the button the Chrome console shows this:
VM596:1 POST http://localhost/wptest2/wp-admin/admin-ajax.php 400 (Bad Request)
(anonymous) @ VM596:1
VM596:1 XHR failed loading: POST "http://localhost/wptest2/wp-admin/admin-ajax.php".
createError.js:16 Uncaught (in promise) Error: Request failed with status code 400
at createError (createError.js:16)
at settle (settle.js:17)
at XMLHttpRequest.handleLoad (xhr.js:59)
Does anyone know what causes these errors and how to handle them?
Also tried:
- Submitting the form data as a JSON object. Same console entries resulted.
1 Answer
Reset to default 2Solved by adding an action to handle unauthenticated users:
add_action('wp_ajax_nopriv_xhrLink', 'handleXhrLink');
本文标签: 400 Bad Request when sending XHR from Reactjs to adminajaxphp
版权声明:本文标题:400 Bad Request when sending XHR from React.js to admin-ajax.php 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741289721a2370478.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论