我正在使用发件人。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();
}
?>