我对jQuery和AJAX尤其陌生。我有一个小问题,返回值总是0,尽管我认为这实际上是成功消息,它没有返回任何内容。
我已经搜索了Google verse,在PHP回调上有die()函数,我相信add\\u操作是正确的。
我在一个本地主机上工作,但我怀疑这会影响它,这都是在管理中,而不是在前端。我还检查了js是否已排队并本地化。
我在chrome开发者区收到一条200 OK的消息。
我还测试了http://codex.wordpress.org/AJAX_in_Plugins 它还返回了0,这让我怀疑它是否不是下面概述的代码。
现在我只是想让它把一些东西发送回jQuery。任何帮助都将不胜感激。
The jQuery
jQuery(document).ready(function(){
jQuery(\'.cl_link_buttons\').val(\'id\').click(function() {
var currentid = jQuery(this).attr(\'id\');
//alert(currentid);
console.log(currentid);
jQuery.ajax ( data = {
action: \'cleanlinks_ajax_get_post_data\',
url: ajaxurl,
type: \'POST\',
dataType: \'text\',
"currentid" : currentid
});
jQuery.post(ajaxurl, data, function(response) {
var dataz = response;
alert( dataz );
console.log (dataz); //show json in console
});
return false;
}); //end click event
}); //end doc ready
The PHP
add_action("wp_ajax_cleanlinks_ajax_get_post_data", "cleanlinks_ajax_get_post_data");
add_action("wp_ajax_nopriv_cleanlinks_ajax_get_post_data", "cleanlinks_ajax_get_post_data");
function cleanlinks_ajax_get_post_data() {
$from_ajax = $_POST[\'currentid\'];
echo "do" . $from_ajax . "something";
die();
}
SO网友:Mcart
我也有同样的问题。解决了这个问题。您必须发送“action”变量,如示例所示:
var dataString = {lat: \'55.56\', lng: \'25.35\', action:\'report_callback\'};
$.ajax({
url: "http://domain.net/wp-admin/admin-ajax.php",
type: "POST",
//some times you cant try this method for sending action variable
//action : \'report_callback\',
data:dataString,
success: function(data){
console.log(data);
},
error: function() {
console.log("Error");
}
});
因为在wp-admin/admin-ajax中。php是操作变量的处理程序:
if ( empty( $_REQUEST[\'action\'] ) ) {...}
Line 26
SO网友:Ngocheng
jQuery(document).ready(function(){
jQuery(\'.cl_link_buttons\').val(\'id\').click(function() {
$.ajax({
type:\'POST\',
url: ajaxurl,
data: {
action : \'ajax_filter\',
currentid : \'currentid\'
},
success: function (result) {
console.log(result);
$result = $(result);
$result.fadeIn(\'7000\');
$("#showresults").html(result);
},
error: function (xhr, status) {
alert("Sorry, there was a problem!");
},
complete: function (xhr, status) {
$(\'#showresults\').slideDown(\'slow\')
}
});
});
});
//代码函数php
<?php
add_action( \'wp_ajax_nopriv_ajax_filter\', \'ajax_filter\' );
add_action( \'wp_ajax_ajax_filter\', \'ajax_filter\' );
function ajax_filter(){
$date = isset($_POST[\'date\']) ? $_POST[\'date\'] : 0;
echo $date;
die();
}
?>
SO网友:Alain-Aymerick FRANCOIS
如果不使用wp\\u localize\\u script()函数设置ajax url,admin ajax将返回0。我想是Wordpress的bug。以下是一个示例:
wp_enqueue_script( \'search_js\', get_template_directory_uri() . \'/js/search.js\', array( \'jquery\' ), null, true );
wp_localize_script( \'search_js\', \'ajaxurl\', admin_url( \'admin-ajax.php\' ) );
javascript文件(search.js):
$(\'#search_input\').autocomplete({
source: function(request, response) {
$.ajax({
type: \'POST\',
dataType: \'json\',
url: ajaxurl,
data: \'action=my_custom_action_search&search_criteria=\' + request.term,
success: function(data) {
response(data);
},
error: function(errorThrown){
console.log(errorThrown);
}
});
},
minLength: 3
});
SO网友:Ly Van Vu
YOU TRY:
add_action(\'init\', \'ly_form_ajax_init\');
function ly_form_ajax_init() {
wp_register_script(\'ly-form-ajax-script\', plugins_url().\'/ly-form/js/ly-script.js\' , array(\'jquery\'));
wp_enqueue_script(\'ly-form-ajax-script\');
wp_localize_script(\'ly-form-ajax-script\', \'ly_form_ajax_object\', array(
\'ajaxurl\' => admin_url(\'admin-ajax.php\'),
\'redirecturl\' => home_url(),
\'loadingmessage\' => __(\'\')
));
}
// Action is: contact_ajax
add_action( \'wp_ajax_contact_ajax\', \'my_function\' );
add_action( \'wp_ajax_nopriv_contact_ajax\', \'my_function\' );
function my_function(){
ob_clean();
echo "http://sanvatvungcao.com";
wp_die();
}
/**
* Short code in page like this: [ly-form]
* @param type $atts
* @param type $content
* @return string
*/
function ly_form_shortcode($atts, $content = "") {
echo html_form_code();
}
add_shortcode(\'ly-form\', \'ly_form_shortcode\');
//HTML Form will show,
function html_form_code() {
$html = "";
$html.= \'\';
$html.= \'\';
$html.= \' Họ đệm *
\';
$html.= \' Tên *
\';
$html.= \' Địa chỉ *
\';
$html.= \' Email *
\';
$html.= \' Nội dung * dg
\';
$html.= \' \';
$html.= \'\';
$html.= \'\';
$html.= \'\';
return $html;
}
AND HERE js (ly-script.js):
( function( $ ) {
$(document).ready(function () {
// Perform AJAX form submit
$(\'form.ly-form-ex\').on(\'submit\', function(e){
e.preventDefault();
$(\'#loading\').html(\'loading...\');
var dataString = {action:\'contact_ajax\'};
$.ajax({
type: "POST",
url: ly_form_ajax_object.ajaxurl,
data: dataString,
success: function (data) {
$(\'#loading\').html(data);
},
error: function (errorThrown) {
alert(errorThrown);
}
});
});
}); // end ready
} )( jQuery );
希望这对你有帮助,贝斯特