admin管理员组文章数量:1128522
I have a problem with AJAX returning 0 always!
I have done everything by the book and can't figure out what is wrong? Please help!!
Here is my Ajax call:
//Pass data through AJAX
var amountToConvert = price;
jQuery.ajax({
type:"POST",
url: "../../wp-admin/admin-ajax.php", // our PHP handler file
action: "ajaxConversion",
data: {
amount: amountToConvert
},
success:function(data){
alert(data);
},
error: function(errorThrown){
alert(errorThrown);
}
});
return false;
And the function in functions.php is:
function ajaxConversion(){
$amount = mysql_real_escape_string($_POST['amount']);
echo $amount;
die();
};
add_action('wp_ajax_nopriv_ajaxConversion', 'ajaxConversion');
add_action('wp_ajax_ajaxConversion', 'ajaxConversion');
I have a problem with AJAX returning 0 always!
I have done everything by the book and can't figure out what is wrong? Please help!!
Here is my Ajax call:
//Pass data through AJAX
var amountToConvert = price;
jQuery.ajax({
type:"POST",
url: "../../wp-admin/admin-ajax.php", // our PHP handler file
action: "ajaxConversion",
data: {
amount: amountToConvert
},
success:function(data){
alert(data);
},
error: function(errorThrown){
alert(errorThrown);
}
});
return false;
And the function in functions.php is:
function ajaxConversion(){
$amount = mysql_real_escape_string($_POST['amount']);
echo $amount;
die();
};
add_action('wp_ajax_nopriv_ajaxConversion', 'ajaxConversion');
add_action('wp_ajax_ajaxConversion', 'ajaxConversion');
Share
Improve this question
asked Aug 30, 2013 at 10:23
KukaKuka
2871 gold badge5 silver badges6 bronze badges
1
|
7 Answers
Reset to default 16Could you place the action (ajaxConversion) in your Data and check?
jQuery.ajax({
type:"POST",
url: ajaxurl,
data: {
action: "ajaxConversion",
amount: amountToConvert
},
success:function(data){
alert(data);
},
error: function(errorThrown){
alert(errorThrown);
}
});
using wp_die();
at the end of AJAX function fixed the issue for me.
e.g
add_action( 'wp_ajax_my_ajax_function', 'my_ajax_function' );
function my_ajax_function(){
echo json_encode($myvar);
wp_die();
}
For me the trick was to add wp_ajax_nopriv
action. I tested the script on one browser when I was logged in WP admin, and then I tried same script in Chrome and realized that the script doesn't work. After I put wp_ajax_nopriv
, everything started to work. :)
add_action( 'wp_ajax_nopriv_erase_uploaded_images', 'erase_uploaded_images' );
add_action( 'wp_ajax_erase_uploaded_images', 'erase_uploaded_images' );
function erase_uploaded_images() {
$attach_id = filter_input( INPUT_POST, 'attach_id' );
wp_delete_attachment( $attach_id );
if ( isset( $_SESSION['uploaded_images'] ) ) {
$array_attachments = $_SESSION['uploaded_images'];
if ( ( $key = array_search( $attach_id, $array_attachments ) ) !== false ) {
unset( $array_attachments[$key] );
}
$_SESSION['uploaded_images'] = $array_attachments;
}
wp_die();
}
I would recommend using wp_send_json_success() and wp_send_json_error() on server side. You don't need to worry about die() etc and the "status" variable is sent automatically, it's much cleaner this way. For example
function ajaxConversion(){
// ...
wp_send_json_success(array(
'amount' => $amount
));
}
Will result in something like this:
{
"success":true,
"data":{"amount":125}
}
So then you can do easily extract the values in your ajax call:
jQuery.ajax({
type : 'post',
data : {
action: 'ajaxConversion',
//nonce : ajax.nonce
},
dataType : 'json',
url : ajax.ajaxurl,
success : function(data){
if(data.success) {
alert(data.amount);
} else {
alert(data.data.message);
}
}
});
Another common thing i've ran into are typos in the action name. They should be wp_ajax_nopriv_{action} or wp_ajax_{action} when logged in. For example, wp-ajax_nopriv, is one I've done several times in the past.
For me it was the fact that I was using return
instead of echo
in my PHP function. Changing it to echo
fixed it.
function doAjax() {
$result = getPosts();
echo json_encode($result, true);
die();
}
You may forget to add below line:
add_action("wp_ajax_your_ajax_function", "your_ajax_function");
Without above line, the ajax it not be fired.
Here is your ajax function
function your_ajax_function(){ $result = json_encode($result); echo $result; }
important think do,hook action correctly add function.php
i face same issues ,i try this all methods,but simple think i
missed require my action file to functions.php
add_action( 'wp_ajax_nopriv_erase_uploaded_images', 'erase_uploaded_images' );
add_action( 'wp_ajax_erase_uploaded_images', 'erase_uploaded_images' );
function erase_uploaded_images() {
$attach_id = filter_input( INPUT_POST, 'attach_id' );
wp_delete_attachment( $attach_id );
if ( isset( $_SESSION['uploaded_images'] ) ) {
$array_attachments = $_SESSION['uploaded_images'];
if ( ( $key = array_search( $attach_id, $array_attachments ) ) !== false ) {
unset( $array_attachments[$key] );
}
$_SESSION['uploaded_images'] = $array_attachments;
}
wp_die();
}
本文标签: functionsAjax call always returns 0
版权声明:本文标题:functions - Ajax call always returns 0 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736727086a1949784.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
admin_url( 'admin-ajax.php' )
to get the AJAX URL, not some made-up URL. – fuxia ♦ Commented Aug 30, 2013 at 10:26