简体   繁体   English

在wordpress插件中使用wp_remote_get时,Ajax调用失败

[英]Ajax call fails when using wp_remote_get in wordpress plugin

I am having issues with wp_remote_get in my Wordpress plugin. 我在Wordpress插件中wp_remote_get问题。

What I want to do is call a method inside my main public class with ajax. 我想要做的是使用ajax在我的主公共类中调用一个方法。 But the thing is that the call fails when the wp_remote_get function is used in it. 但问题是当在wp_remote_get使用wp_remote_get函数时调用失败。 It is supposed to do an API call and return the data to the jQuery. 它应该进行API调用并将数据返回给jQuery。 When I comment out the wp_remote_get the call works fine and response is given back. 当我注释掉wp_remote_get ,调用工作正常并且返回响应。 Any ideas how can I make this work? 任何想法我怎样才能使这项工作?

Method that processes the call: 处理调用的方法:

    public function countryLookupApiCall() {
    if (isset($_POST['action']) && isset($_POST['country'])) {
        $country = $_POST['country'];
        $apiKey = $this->getApiKey();
        $url = $this->url . $country . '/callCharges?apiKey=' . $apiKey . '&response=JSON';
        $response = wp_remote_get($url);
        echo $response;
        die();
        }
    }

jQuery: jQuery的:

jQuery(document).ready(function() {
jQuery("#countryLookupForm").submit(function(e){
    var country = jQuery("#selectCountry").val();
    var action = 'countryLookupResponse';

    jQuery.ajax ({
        type: 'POST',
        url: countryLookup.ajaxurl,
        dataType: 'json',
        data: {action: action, country: country},

        success: function(data) {
            //do something with this data later on
            var result = jQuery.parseJSON(data);
            }
        });
    });
});

Wordpress actions are all registered well because the call works when I don't use the wp_remote_get Wordpress操作都注册良好,因为当我不使用wp_remote_get时调用有效

EDIT: The solution was more than simple, I just needed to add e.preventDefault(); 编辑:解决方案不仅仅是简单,我只需要添加e.preventDefault();

You need to add errors checking into your code. 您需要在代码中添加错误检查。 This can help you to figure out what is causing the problem. 这可以帮助您找出导致问题的原因。

    public function countryLookupApiCall() {
if (isset($_POST['action']) && isset($_POST['country'])) {
    $country = $_POST['country'];
    $apiKey = $this->getApiKey();
    $url = $this->url . $country . '/callCharges?apiKey=' . $apiKey . '&response=JSON';
    $response = wp_remote_get($url);
    if (is_wp_error($response)) {
        $error_code = $response->get_error_code();
        $error_message = $response->get_error_message();
        $error_data = $response->get_error_data($error_code);
        // Process the error here....
    }
    echo $response;
    die();
    }
}

Also you are using echo on wp_remote_get result. 您还在wp_remote_get结果上使用echo As defined in documentation wp_remote_get returs WP_Error or array instance. 如文档中所定义的,wp_remote_get可以返回WP_Error或数组实例。 So you should use something like this: 所以你应该使用这样的东西:

echo $response['body'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM