admin管理员组文章数量:1336632
I'm quite new to doing api calls using Wordpress so bear with me.
I have this function:
function foo() {
$userinfo = [
"number" => "26246426",
"secondnumber" => "43634643"
];
$api_url = '';
$response = wp_remote_post( $api_url, array(
// Set the Content-Type header.
'headers' => array(
'Content-Type' => 'application/json',
),
// Set the request payload/body.
'body' => json_encode( $userinfo ),
) );
$res_body = wp_remote_retrieve_body( $response );
echo $res_body;
die();
}
This works and I get a response.
Now what I'm trying to do is send the response in an action towards my ajax that's in a script right under the php function from above (everything is happening inside a plugin file)
Here are my actions
add_action('wp_ajax_add_foo', 'foo' );
add_action('wp_ajax_nopriv_add_foo', 'foo' );
And my ajax call inside a script tag under the function
var ajaxscript = { ajax_url : 'mywebsite/wp-admin/admin-ajax.php' }
jQuery(document).ready(function($) {
$.ajax ({
url: ajaxscript.ajax_url,
type: 'POST',
// dataType: 'application/json',
data: {
// the value of data.action is the part AFTER 'wp_ajax_' in
// the add_action ('wp_ajax_xxx', 'yyy') in the PHP above
action: '_add_foo'
// ANY other properties of data are passed to your_function()
// in the PHP global $_REQUEST (or $_POST in this case)
},
success : function( response ){ console.log(response) },
error : function(error){ console.log(error) }
}) ;
});
I would be expecting to get back the $res_body variable but I'm getting a 400. Why is that?
I'm quite new to doing api calls using Wordpress so bear with me.
I have this function:
function foo() {
$userinfo = [
"number" => "26246426",
"secondnumber" => "43634643"
];
$api_url = 'https://api.url';
$response = wp_remote_post( $api_url, array(
// Set the Content-Type header.
'headers' => array(
'Content-Type' => 'application/json',
),
// Set the request payload/body.
'body' => json_encode( $userinfo ),
) );
$res_body = wp_remote_retrieve_body( $response );
echo $res_body;
die();
}
This works and I get a response.
Now what I'm trying to do is send the response in an action towards my ajax that's in a script right under the php function from above (everything is happening inside a plugin file)
Here are my actions
add_action('wp_ajax_add_foo', 'foo' );
add_action('wp_ajax_nopriv_add_foo', 'foo' );
And my ajax call inside a script tag under the function
var ajaxscript = { ajax_url : 'mywebsite/wp-admin/admin-ajax.php' }
jQuery(document).ready(function($) {
$.ajax ({
url: ajaxscript.ajax_url,
type: 'POST',
// dataType: 'application/json',
data: {
// the value of data.action is the part AFTER 'wp_ajax_' in
// the add_action ('wp_ajax_xxx', 'yyy') in the PHP above
action: '_add_foo'
// ANY other properties of data are passed to your_function()
// in the PHP global $_REQUEST (or $_POST in this case)
},
success : function( response ){ console.log(response) },
error : function(error){ console.log(error) }
}) ;
});
I would be expecting to get back the $res_body variable but I'm getting a 400. Why is that?
Share Improve this question asked May 20, 2020 at 15:55 ionitalexionitalex 112 bronze badges2 Answers
Reset to default 0I figured this out after a night sleep. Sometimes our brains function like that.
The problem was actually my call here, It should've been "add_foo" not "_add_foo"
data: {
// the value of data.action is the part AFTER 'wp_ajax_' in
// the add_action ('wp_ajax_xxx', 'yyy') in the PHP above
action: '_add_foo'
// ANY other properties of data are passed to your_function()
// in the PHP global $_REQUEST (or $_POST in this case)
},
Your ajax url is incorrect.
Try changing:
var ajaxscript = { ajax_url : 'mywebsite/wp-admin/admin-ajax.php' }
To:
var ajax_url = "<?php echo admin_url('admin-ajax.php'); ?>";
And change: url: ajaxscript.ajax_url,
to url: ajax_url,
But the proper way to do this is by using wp_localize_script
本文标签: pluginsWordpress api call using wpajax returns error 400
版权声明:本文标题:plugins - Wordpress api call using wp-ajax returns error 400 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742414048a2470364.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论