首页 > 解决方案 > 在 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 端点。

标签: phpajaxwordpress

解决方案


推荐阅读