WordPress AJAX错误400向远程站点发送数据的错误请求

时间:2018-11-13 作者:user3364610

我正在使用发件人。net获取电子邮件订阅列表。这个网站给了我一些信息,可以将用户的电子邮件添加到订阅列表中。我想使用WordPress ajax来实现这一点。但它返回错误400错误请求。我的代码是:

文件ajax新闻脚本。js公司:

    jQuery(document).ready(function($){

    // Perform AJAX send news on form submit
    $(\'form#fnews\').on(\'submit\', function(e){

        $(\'form#fnews #fsubmit\').text(ajax_news_object.loadingmessage);
        $.ajax({
            type: \'POST\',
            dataType: \'json\',
            url: ajax_news_object.ajaxurl,
            data: { 
                \'action\': \'ajaxnews\', //calls wp_ajax_nopriv_ajaxnews
                \'email\': $(\'form#fnews #femail\').val(),  
                \'security\': $(\'form#fnews #security\').val() },

            success: function(data){
                $(\'form#fnews #fsubmit\').text(data.message);
              }
            });
            e.preventDefault();
        });
    });
在阿贾克斯新闻中。php文件:

    <?php

add_action( \'init\', \'ajax_news_init\' );

function ajax_news_init() {
        global $rf_data;
        wp_register_script( \'ajax-news-script\', get_stylesheet_directory_uri() . \'/js/ajax-news-script.js\', array(\'jquery\'), \'1.0\', true );
        wp_enqueue_script( \'ajax-news-script\' );

        wp_localize_script( \'ajax-news-script\', \'ajax_news_object\', array(
            \'ajaxurl\' => admin_url( \'admin-ajax.php\' ),
            \'loadingmessage\' => __( \'لطفا صبر کنید ...\' )
        ));

        add_action( \'wp_ajax_nopriv_ajax_news\', \'ajax_news\' );
        add_action( \'wp_ajax_ajax_news\', \'ajax_news\' );
    }

// Check if users input information is valid
    function ajax_news() {

        global $rf_data;

        // First check the nonce, if it fails the function will break
        check_ajax_referer( \'ajax-news-nonce\', \'security\' );

    $info = array();
    $info[\'email\'] = $_POST[\'email\'];

    $api_key = "my api key";
    $list_id = my list id;
    $url = \'https://app.sender.net/api\'; 

    $data = array(
        "method" => "listSubscribe",
        "params" => array(
            "api_key" => $api_key,
            "list_id" => $list_id,
            "emails" => array($info[\'email\']) 
        )
    );

$options = array(
    \'http\' => array( 
        \'method\' => \'POST\',
        \'content\' => http_build_query(array(\'data\' => json_encode($data))) 
    )
); 

$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$response = json_decode($result);

    if (!empty($response[\'success\'])  ){
            echo json_encode( array( \'send\'=>true, \'message\'=>__( \'Yes!\' )));
        } else {
            echo json_encode( array( \'send\'=>false, \'message\'=>__( \'No!\' ) ));
        }
        die();
    }

    ?>

1 个回复
SO网友:Jacob Peattie

您的操作名称和挂钩不匹配。你已经迷上了_ajax_news 在挂钩名称中:

add_action( \'wp_ajax_nopriv_ajax_news\', \'ajax_news\' );
add_action( \'wp_ajax_ajax_news\', \'ajax_news\' );
但是您在JavaScript中发送的操作是ajaxnews, 不带下划线:

\'action\': \'ajaxnews\', //calls wp_ajax_nopriv_ajaxnews
查看您自己的注释与实际使用的挂钩的名称有何不同?

JavaScript需要发送ajax_news 作为操作名称,或钩子需要wp_ajax_nopriv_ajaxnews. 他们只需要保持一致。

结束

相关推荐

Ajax call not working anymore

我用ajax调用了一些函数。。。基本上,一切都按预期进行,但现在不是这样。我不确定是什么导致了这个问题。我的页面上有这样一个按钮:<p class=\'form-submit\' method=\'post\'> <input id=\'$message_id\' name=\'message_read\' type=\'button\' onclick=hideMessage($message_id) class=\'submit button mark-as-read