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
1 Answer
Reset to default 0Your 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.