jQuery Ajax:为什么成功显示0?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5371393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:03:24  来源:igfitidea点击:

Ajax : Why success displays 0?

jqueryajaxwordpress

提问by Steffi

I use ajax with jquery and when I tried to display the result in popup. Then alert always displays 0

我将 ajax 与 jquery 一起使用,当我尝试在弹出窗口中显示结果时。然后警报总是显示0

success : function(results) { 
alert(results); }

EDIT :

编辑 :

 var dataToSend = {
    action:"ACTION_NAME",
    id : "12"
  };
  url_action = 'http://www.______.com/wp-admin/admin-ajax.php';
  $.ajax({
          url: url_action,
          type:'POST',
          data: dataToSend,
          success:function(results)
          {
             alert(results);
          }
   });
 var dataToSend = {
    action:"ACTION_NAME",
    id : "12"
  };
  url_action = 'http://www.______.com/wp-admin/admin-ajax.php';
  $.ajax({
          url: url_action,
          type:'POST',
          data: dataToSend,
          success:function(results)
          {
             alert(results);
          }
   });

回答by zourbuth

Try to add die();or exit();at the very last line of the function.

尝试在函数的最后一行添加die();exit();

回答by Wasi Abbas

May be you are trying without login. so you need to use this action.

可能是您在没有登录的情况下尝试。所以你需要使用这个动作。

add_action('wp_ajax_nopriv_my_action', 'my_action_callback');

add_action('wp_ajax_nopriv_my_action', 'my_action_callback');

you will get response. :)

你会得到回应。:)

回答by mart

I had a similar issue with admin-ajax.php returning 0, even i had die().

我有一个类似的问题,admin-ajax.php 返回 0,即使我有死()。

My problem was that the add_action was declared in a file not loaded default by plugin.

我的问题是 add_action 是在插件默认未加载的文件中声明的。

Anyway, if you have problem also check in browser what __.com/wp-admin/admin-ajax.php?action=youraction returns.

无论如何,如果您有问题也请检查浏览器 __.com/wp-admin/admin-ajax.php?action=youraction 返回什么。

回答by Steffi

Found it.

找到了。

It need to add die();before the end of my own ajax function in function.php.

它需要die();在我自己的 ajax 函数结束之前添加在function.php.

Because there is one line of script in admin-ajax.phpafter my own ajax_action that says: die('0'); So we need to die()script before die('0').

因为admin-ajax.php在我自己的 ajax_action 之后有一行脚本说: die('0'); 所以我们需要在 die('0') 之前使用die()脚本。

回答by Protector one

I was adding the "action" incorrectly. You have to prefix your action with wp_ajax_gd_.

我错误地添加了“动作”。您必须在您的操作前加上wp_ajax_gd_.

add_action('wp_ajax_gd_[ACTION NAME]', '[CALLBACK FUNCTION NAME]')

and in the JS jQuery post:

并在 JS jQuery 帖子中:

var data = {
    'action': '[ACTION NAME]',
};

I found Wordpress' documentation very unclear about this part.

我发现 Wordpress 的文档对此部分非常不清楚。

回答by Darin Dimitrov

That's probably because the server side script you are calling with AJAX returns 0(the one you have specified in the urlproperty).

这可能是因为您使用 AJAX 调用的服务器端脚本返回0(您在url属性中指定的脚本)。

回答by caffre

Make sure the action_name is ok. If it's not found in functions.php you will get 0 as the result.

确保 action_name 没问题。如果在functions.php 中没有找到它,你将得到0 作为结果。

回答by Seano

In your PHP function, make sure you're using echoinstead of return.

在您的 PHP 函数中,确保您使用的echoreturn.

This was happening for me in a WordPress theme.

这发生在我的 WordPress 主题中。

function doAjax() {
    $result = getPosts();
    echo json_encode($result, true);
    die();
}

回答by James Newell

I had the same issue. I'd forgotten to enable the plugin. After enabling the plugin I received the expected data.

我遇到过同样的问题。我忘记启用插件了。启用插件后,我收到了预期的数据。

回答by Sava? Has?elik

Those who get error 0 :), action => 'action'

那些得到错误 0 :), action => 'action'

var data = {'action': 'my_action', 'data': form_data };

$.post(ajaxurl, data, function(response) { alert(response);});