$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugins - Wordpress ajax error 400 bad request for sending data to remote site|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugins - Wordpress ajax error 400 bad request for sending data to remote site

matteradmin8PV0评论
This question already has an answer here: AJAX call using admin-ajax URL is returning 400 bad request (1 answer) Closed 6 years ago.

I am using Sender for email subscription lists. This site give me some information to add user's emails to subscription list. I want to use WordPress ajax to for this. but it return error 400 bad request. My codes is:

File ajax-news-script.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();
        });
    });

And in ajax-news.php file:

    <?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 = ''; 

    $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();
    }

    ?>
This question already has an answer here: AJAX call using admin-ajax URL is returning 400 bad request (1 answer) Closed 6 years ago.

I am using Sender for email subscription lists. This site give me some information to add user's emails to subscription list. I want to use WordPress ajax to for this. but it return error 400 bad request. My codes is:

File ajax-news-script.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();
        });
    });

And in ajax-news.php file:

    <?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/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();
    }

    ?>
Share Improve this question asked Nov 13, 2018 at 9:45 user3364610user3364610 112 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 0

Your action name and hooks don't match. You have hooked to _ajax_news in the hook name:

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

But the action you are sending in JavaScript is ajaxnews, without the underscore:

'action': 'ajaxnews', //calls wp_ajax_nopriv_ajaxnews

See how your own comment has a different name than the actual hooks you've used?

Either the JavaScript needs to send ajax_news as the action name, or the hooks need to be wp_ajax_nopriv_ajaxnews. They just need to be the same.

Post a comment

comment list (0)

  1. No comments so far