php - 在 wordpress 中实现 POST API 调用
问题描述
我正在尝试向 API 发出 POST 请求。我在我的主题中创建了一个 javascript 文件,到目前为止我已经尝试了两种方法:
方法一:
$.ajax({
headers:{
"Content-Type":"application/json"
},
data:{
"command": "request",
"data": {
"api_key": "my_api_key",
"api_username": "my_api_username",
"vendor_type": 2,
"from": {
"from_name": "",
"from_lat": "",
"from_long": "",
"from_description": ""
},
"to": {
"to_name": customerLocation,
"to_lat": cityLat,
"to_long": cityLng,
"to_description": ""
},
"recepient": {
"recepient_name": name,
"recepient_phone": phone,
"recepient_email": email,
"recepient_notes": ""
},
"delivery_details": {
"pick_up_date": pickupdate,
"collect_payment": {
"status": false,
"pay_method": 0,
"amount": amountTotal
},
"return": true,
"note": " Sample note",
"note_status": true,
"request_type": "quote"
}
},
"request_token_id": requestToken
},
type: 'POST',
url:"https://apitest.sendyit.com/v1/",
success: function(response){
console.log('success');
console.log(response);
},
error: function(response){
console.log('error');
console.log(response);
}
});
上述代码不会从 API 返回响应负载,因为 CORS(跨域资源共享)将阻止任何调用
方法二
然后我尝试将端点传递给我的functions.php
文件,如下所示:
//make api call to sendy
function foo()
{
echo 'https://apitest.sendyit.com/v1/';
}
add_action('wp_ajax_foo', 'foo' );
add_action('wp_ajax_nopriv_foo', 'foo' );
ajaxUrl
我在主题的 header.php 文件中声明了一个变量:
<script type="text/javascript">
var ajaxUrl = "<?php echo admin_url('admin-ajax.php'); ?>";
</script>
然后格式化我的 Ajax 调用,如下所示:
$.ajax({
headers:{
"Content-Type":"application/json"
},
data:{
"action": 'foo',
"command": "request",
"data": {
"api_key": "my_api_key",
"api_username": "my_api_username",
"vendor_type": 2,
"from": {
"from_name": "",
"from_lat": "",
"from_long": "",
"from_description": ""
},
"to": {
"to_name": customerLocation,
"to_lat": cityLat,
"to_long": cityLng,
"to_description": ""
},
"recepient": {
"recepient_name": name,
"recepient_phone": phone,
"recepient_email": email,
"recepient_notes": ""
},
"delivery_details": {
"pick_up_date": pickupdate,
"collect_payment": {
"status": false,
"pay_method": 0,
"amount": amountTotal
},
"return": true,
"note": " Sample note",
"note_status": true,
"request_type": "quote"
}
},
"request_token_id": requestToken
},
type: 'POST',
url:ajaxUrl,
success: function(response){
console.log('success');
console.log(response);
},
error: function(response){
console.log('error');
console.log(response);
}
});
但这给了我一个400 (Bad Request)
错误。
我应该提到我已经在文件中正确设置了我的 javascriptfunctions.php
文件:
wp_enqueue_script('sendy', get_template_directory_uri() . '/assets/js/sendy.js', array('jquery'));
wp_localize_script('sendy', 'wc_checkout_params', array('ajaxurl' => admin_url('admin-ajax.php')));
因此想请求帮助设置我的 POST 端点。
解决方案
推荐阅读
- java - 在哪里保存 logback.xml 文件以在 Java 中使用 Logger slf4j
- python - 编写 Python 脚本 - GUI 选项?
- python - Pandas sort_values 函数占用大量内存,无需任何赋值操作
- java - Android Studio 在更改布局后使用 setOnTouchListener
- angular - 在孩子中访问父母的ngForm
- git - 致命:无法访问“.....”:gnutls_handshake() 失败:握手失败
- java - 如果 POJO 类的属性之一为空,如何在将 JSON 映射到 POJO 时处理 500 内部错误
- ios - 谷歌登录 iOS 崩溃?
- ruby-on-rails - 使用 RSpotify Gem 和 Spotify 在 URI 中包含一个查询字符串,这使其成为未经批准的源、rails、heroku
- c - crypt() 不返回标准 MD5 哈希?