I have on website on wordpress and a external web app. I try to make an AJAX call from this external web app to get some info from my wordpress site/ database but i got an error message 400.
Here is my ajax call code :
jQuery.ajax({
type:"POST",
url: ".php",
contentType: 'json',
dataType: "JSON",
responseType:'json',
data: {
action : "itempricingfunction",
ean : "EANTEST0101010"
},
success:function(data){
alert(data);
},
error: function(errorThrown){
console.log(errorThrown);
}
});
and here is my functions.php file inside my wordpress website :
function itempricingfunction(){
$product_ean = (isset($_POST['ean'])) ? htmlentities($_POST['ean']) : NULL;
echo json_encode(array('product_ean' =>$product_ean));
exit;
}
add_action("wp_ajax_nopriv_itempricingfunction", "itempricingfunction");
add_action("wp_ajax_itempricingfunction", "itempricingfunction");
The response from wordpress is error 404.
Can you help me to communicate and set/get some data from my worpdress website please ?
Thank you in advance.
This question already exists: AJAX request from Chrome Extension to Wordpress Website Closed 5 years ago.I have on website on wordpress and a external web app. I try to make an AJAX call from this external web app to get some info from my wordpress site/ database but i got an error message 400.
Here is my ajax call code :
jQuery.ajax({
type:"POST",
url: "https://www.groupio.fr/wp-admin/admin-ajax.php",
contentType: 'json',
dataType: "JSON",
responseType:'json',
data: {
action : "itempricingfunction",
ean : "EANTEST0101010"
},
success:function(data){
alert(data);
},
error: function(errorThrown){
console.log(errorThrown);
}
});
and here is my functions.php file inside my wordpress website :
function itempricingfunction(){
$product_ean = (isset($_POST['ean'])) ? htmlentities($_POST['ean']) : NULL;
echo json_encode(array('product_ean' =>$product_ean));
exit;
}
add_action("wp_ajax_nopriv_itempricingfunction", "itempricingfunction");
add_action("wp_ajax_itempricingfunction", "itempricingfunction");
The response from wordpress is error 404.
Can you help me to communicate and set/get some data from my worpdress website please ?
Thank you in advance.
Share Improve this question edited Jan 20, 2020 at 20:52 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Jan 20, 2020 at 20:33 Tayfun AkaltunTayfun Akaltun 11 silver badge3 bronze badges 2- Does this answer your question? AJAX request from Chrome Extension to Wordpress Website – Antti Koskinen Commented Jan 21, 2020 at 7:15
- Is the error 400 or 404? You've said both, but they're different errors that mean different things. It's important to be precise. – Jacob Peattie Commented Jan 21, 2020 at 7:31
1 Answer
Reset to default 0You shouldn't be sending your data as JSON:
dataType: "JSON"
For add_action("wp_ajax_nopriv_itempricingfunction"
to work, WordPress needs to find the action
parameter in your request, which you're correctly setting here:
data: {
action : "itempricingfunction",
ean : "EANTEST0101010"
},
The problem is that it checks this using $_REQUEST['action']
, but PHP doesn't populate $_REQUEST
with JSON. It only works with form data. So remove the dataType
line and you should be fine:
jQuery.ajax(
{
type: 'POST',
url: 'https://www.groupio.fr/wp-admin/admin-ajax.php',
responseType: 'json',
data: {
action: 'itempricingfunction',
ean: 'EANTEST0101010'
},
success: function( data ) {
alert( data );
},
error: function( errorThrown ) {
console.log( errorThrown );
}
}
);
There's a couple of other things to consider too:
- If the web app is on a different domain, you may run into CORS issues.
- A custom REST API endpoint would be more appropriate for this sort of thing, and does support receiving data as JSON.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744816677a4595367.html
评论列表(0条)