Admin Ajax返回0
-
-
您是否已验证ajaxurl设置正确?Have you verified that ajaxurl is set properly?
- 1
- 2013-04-27
- Andrew Bartel
-
浏览器控制台是否显示任何错误?如果是这样,它们是什么?Does your browser console show any errors? If so, what are they?
- 0
- 2013-04-27
- s_ha_dum
-
jQuery('.cl_link_buttons').val('id').click(function()`看起来很奇怪.`jQuery('.cl_link_buttons').val('id').click(function()` looks odd.
- 2
- 2013-04-27
- fuxia
-
Andrew,是的,我认为这是正确的,Chrome Inspector中的请求网址显示的是domain/wp-admin/admin-ajax.phpAndrew, yes I believe it is correct, the request url in Chrome Inspector is showing domain/wp-admin/admin-ajax.php
- 0
- 2013-04-27
- Apina
-
@s_ha_dum没有错误显示@s_ha_dum No errors showing
- 0
- 2013-04-27
- Apina
-
@toscho也许看起来确实很奇怪,这是我可以想到的在单击时获取元素ID的唯一方法,并且确实可以完成此工作,代码还显示了它具有该数据,再次出现的问题是从php获取响应.@toscho Perhaps it does look odd, it was the only way I could think of getting the elements ID on click, and it does do the job, the code also shows it has that data, again issue is getting a response from the php.
- 0
- 2013-04-27
- Apina
-
另请参阅[如何使用HTTP调试AJAX](http://wordpress.stackexchange.com/a/96556/73).See also [how to debug AJAX with HTTP](http://wordpress.stackexchange.com/a/96556/73).
- 0
- 2013-04-27
- fuxia
-
无需"返回false".如果使用jQuery,请改用http://api.jquery.com/event.preventdefault/.There is no need to `return false`. If using jQuery, use http://api.jquery.com/event.preventdefault/ instead.
- 0
- 2015-10-26
- Solomon Closson
-
14 个回答
- 投票数
-
-
你能证明你是怎么做到的吗?can you show how did you do it ?
- 4
- 2014-01-27
- Francisco Corrales Morales
-
我遇到了同样的问题,能否请您说明如何解决此问题?I am having this same issue, could you show how you fixed this?
- 1
- 2014-03-08
- Jeremy
-
另一个答案在这里:http://wordpress.stackexchange.com/a/131397 @杰里米Another answer is here: http://wordpress.stackexchange.com/a/131397 @Jeremy
- 0
- 2014-03-26
- Francisco Corrales Morales
-
-
- 2013-07-27
0
响应意味着未设置动作(在ajax数据中)或找不到该动作的回调函数.A
0
response means either that the action is not set (in the ajax data) or that the action's callback function cannot be found.-
是的,这是正确的答案.最后添加die()的所有操作都是终止脚本.如果您在输出的END后面看到0,则该答案在技术上是正确的,但是,如果您得到的全部是'0',则表示什么都没有返回,并且您有此答案中所述的错误.Yeah, this is the correct answer. All adding die() to the end does, is terminate the script. That answer is technically correct if you're seeing 0 appended to the END of the output, however if all you get is '0', then it means nothing was returned, and you have an error as described in this answer.
- 2
- 2014-05-11
- Hybrid Web Dev
-
或者您只是在处理ajax请求的php中没有故意返回任何内容.确保回显某些内容,否则,请使用.always捕获它.Or you just returned nothing on purpose in the php that handles the ajax request. Be sure to echo something out, otherwise, use `.always` to capture it.
- 0
- 2015-10-26
- Solomon Closson
-
- 2014-01-27
您要做的是在函数的末尾添加
die();
.在此处查看原因和更多信息: http://codex.wordpress.org/AJAX_in_Plugins >
注意:
- 在执行
echo
之前,您应该die
.这样可以防止服务器错误,并且在调试时会有所帮助.
What you have to do is add
die();
at the end of your function.See the reason and more here: http://codex.wordpress.org/AJAX_in_Plugins
Notes:
- You should
echo
something before executingdie
. This will prevent server errors, and will help when debugging.
-
这是WP AJAX 0问题的答案.This is the answer to the WP AJAX 0 problem.
- 10
- 2014-03-26
- Ben Racicot
-
实际上,如果只添加die()而没有回声,这也会给您500内部服务器错误,并为wp-admin/admin-ajax.php返回0.即使您只是设置值而无需返回任何内容,也应始终回显某些内容.否则,如果没有回显任何声音,并且没有die(),则必须使用.always()来捕获它,因为它将不在.done()中,而将在.fail()中.因为它死了没有任何东西=500错误.Actually, if you just add `die()` without echoing something out, this will also give you a 500 Internal Server Error, and return `0` for `wp-admin/admin-ajax.php`. You should always echo out something, even if you are just setting values and nothing is needed to be returned. Otherwise, if you echo nothing and `die()`, you have to use `.always()` to capture it, cause it will not be in `.done()`, it will be in `.fail()` because it dies without anything = 500 Error.
- 0
- 2015-10-26
- Solomon Closson
-
您是否有一些链接或工作代码,以便我们看一下?@SolomonClossondo you have some links, or working code, so we can take a look ? @SolomonClosson
- 0
- 2015-10-26
- Francisco Corrales Morales
-
我所有的答案均已在现场环境中进行了测试.测试它非常简单,只需在functions.php文件中的ajax函数中执行一次die();而无需在此之前回显任何内容,然后通过ajax调用该动作,例如:vartesting=$.ajax(...);testing.fail(function(response){console.log('Failed'+ response);});testing.done(function(response){console.log('Success'+ response);});testing.always(function(response){console.log('Ajax Request complete:'+ response);});`All my answers have been tested in live environments. It's very simple to test this, just do a `die();` in the ajax function in the `functions.php` file without echoing out anything prior to this, and call the action via ajax, e.g.: `var testing = $.ajax( ... ); testing.fail(function(response) { console.log('Failed ' + response); }); testing.done(function(response) { console.log('Success ' + response); }); testing.always(function(response) { console.log('Ajax Request complete: ' + response); });`
- 0
- 2015-10-26
- Solomon Closson
-
您会注意到,将显示"失败",响应将是500 Internal Server Error.You will notice, Failed will show up, the response will be a 500 Internal Server Error.
- 0
- 2015-10-26
- Solomon Closson
-
比在您die();之前在PHP函数中添加回显,响应将是回显的结果.Than add an echo to the PHP function just before you `die();`, the response will than be the echo'd result.
- 0
- 2015-10-26
- Solomon Closson
-
我在将dataType设置为HTML的情况下进行了测试,不确定其他数据类型是否会返回不同的内容,或者未设置时会发生什么,因为它将尝试对数据类型进行有根据的猜测.IMO,最好总是输出一些东西,即使它是一个表示"成功"或"失败"的字符串.I tested this with `dataType` set to `HTML`, not sure if other datatypes return something different tho, or what happens when it is not set, since it will attempt to take an educated guess at the data type. IMO, it's just best to output something always, even if it's a string that says 'success' or 'failure'.
- 0
- 2015-10-26
- Solomon Closson
-
- 2014-05-11
我遇到了同样的问题.并解决了.您必须像示例中一样发送" 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
I got same problem. And solved it. You must send "action" variable like in example:
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"); } });
Because in wp-admin/admin-ajax.php is handler for action variable:
if ( empty( $_REQUEST['action'] ) ) {...} Line 26
-
OP ___确实____发送一个" action"参数.尽管这可能对您有用,但这不是这里的问题.The OP ___does___ send an `action` parameter. While this may have worked for you it was not the problem here.
- 4
- 2014-05-11
- s_ha_dum
-
- 2016-03-29
我也有这个问题,这是我在PHP函数中使用
return
而不是echo
的事实.将其更改为echo
即可解决.function doAjax() { $result = getPosts(); echo json_encode($result, true); die(); }
I had this problem too, and it was the fact that I was using
return
instead ofecho
in my PHP function. Changing it toecho
fixed it.function doAjax() { $result = getPosts(); echo json_encode($result, true); die(); }
-
- 2013-04-27
尝试在控制台上运行此代码
jQuery.post(ajaxurl, {action:'cleanlinks_ajax_get_post_data'}, function(response) { console.log (response); });
我发现您的JavaScript代码有很多错误,这可能就是原因.
Try running this code on the console
jQuery.post(ajaxurl, {action:'cleanlinks_ajax_get_post_data'}, function(response) { console.log (response); });
I can see many things wrong about your JavaScript code and that might be the reason.
-
嗯,这带来了很多我不完全了解的事情.我的理解:ReadyState 4,状态为200,responseText为" 0". 然后给出响应0. 如果有什么具体的我应该在这里寻找? 如果代码有问题,请指出来,我可以研究一下,我仍在学习jQuery.Well it is coming up with a lot of things I dont fully understand. What I do understand: ReadyState 4, status 200, responseText "0". And then it comes up with the response 0. If there something in specific I should be looking for here? If there are issues with the code, please point them out and I can look into them, I am still learning jQuery.
- 0
- 2013-04-27
- Apina
-
您的网站正在运行吗?do you have your site running live?
- 0
- 2013-04-27
- Omar Abid
-
不,这是本地主机No, it's localhost
- 0
- 2013-04-27
- Apina
-
很难说.您可以尝试运行console.info(ajaxurl);看看它能给什么?Hard to tell. Could you try running console.info(ajaxurl); and see what it gives?
- 0
- 2013-04-27
- Omar Abid
-
/wp-admin/admin-ajax.php是响应 编辑- /wp-admin/admin-ajax.php 未定义/wp-admin/admin-ajax.php is the response edit -- /wp-admin/admin-ajax.php undefined
- 0
- 2013-04-27
- Apina
-
尝试用'http://localhost/wp-admin/admin-ajax.php'替换ajaxurl,看看它能提供什么try replacing ajaxurl with 'http://localhost/wp-admin/admin-ajax.php' and see what it gives
- 1
- 2013-04-27
- Omar Abid
-
这不是直接的解决方案,但是这个答案肯定是应该对OP的解决方案进行交叉检查的东西Not a direct solution, but this answer is something one should definitely cross-check for a solution to the OP
- 0
- 2016-01-07
- Nirav Zaveri
-
- 2016-07-18
我遇到了同样的问题,要解决该问题,我使用了
wp_die()
在函数的末尾,仅在echo
之后.不要忘记对脚本进行操作.可以肯定的是,请检查您的函数是否必须使用
wp_ajax_nopriv
,例如wp_ajax
.I had the same problem, to fix it I used
wp_die()
at the end of my function just after anecho
. Don't forget to pass your action on your script.To be sure, check if your function has to use
wp_ajax_nopriv
likewp_ajax
. -
- 2017-04-27
仅供参考,对于任何在这里谷歌搜索" ajax请求返回0"的人: 请记住,当您向对象的方法添加ajax操作时,请确保方法访问修饰符为
public
.add_action( 'wp_ajax_my_action', [$object, 'my_method']);
add_action
无法在$object
之外调用您的方法,它只会静音.Just for reference, for anyone who get here googling "ajax request is returning 0": Remember when you add ajax action to object's method to be sure methods access modifier is
public
.add_action( 'wp_ajax_my_action', [$object, 'my_method']);
add_action
just silences if it can't call your method outside of$object
. -
- 2017-10-17
如果您不使用wp_localize_script()函数设置ajax url,则admin ajax将返回0.我认为这是Wordpress错误.这是一个示例:
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 });
If you don't use wp_localize_script() function to set ajax url, admin ajax returns 0. I think it's Wordpress bug. Here's is an example :
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' ) );
The javascript file (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 });
-
- 2018-03-22
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(); } ?>
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') } }); }); });
//code function 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(); } ?>
-
仅仅发布代码是不好的,您能解释一下这段代码的作用吗?Just posting code is not good, can you please explain what this piece of code does?
- 0
- 2018-03-22
- bravokeyl
-
重要:$ date=isset($ _ POST ['date'])吗?$ _POST ['date']:0; 和功能die();important: $date = isset($_POST['date']) ? $_POST['date'] : 0; And function die();
- 0
- 2018-03-22
- Ngocheng
-
- 2019-11-10
那些得到错误0的人:),action=>'action'
var data = { 'action': 'firmabilgilerikaydet', 'data': form_data }; $.post(ajaxurl, data, function(response) { alert(response); });
Those who get error 0 :), action => 'action'
var data = { 'action': 'firmabilgilerikaydet', 'data': form_data }; $.post(ajaxurl, data, function(response) { alert(response); });
-
- 2020-06-02
如果您使用的是本地主机,并且您的php服务器端代码位于插件文件中,请首先登录管理控制台并刷新插件页面.其次,检查插件是否已激活.然后转到前端并刷新,然后尝试再次发送.
If you are using localhost and your php server side code is in a plugin file first login to admin dashboard and refresh the plugin page. Secondly, check if the plugin is activated. Then go to frontend and refresh and try sending again.
-
- 2017-03-22
你试试: add_action('init','ly_form_ajax_init'); 函数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'=> __('') )); } //动作是:contact_ajax add_action('wp_ajax_contact_ajax','my_function'); add_action('wp_ajax_nopriv_contact_ajax','my_function'); 函数my_function(){ ob_clean(); 回声" http://sanvatvungcao.com"; wp_die(); } /** *页面中的短代码如下:[ly-form] * @参数类型$ atts * @参数类型$ content * @返回字符串 */ 函数ly_form_shortcode($ atts,$ content=""){ 回声html_form_code(); } add_shortcode('ly-form','ly_form_shortcode'); //HTML表单将显示, 函数html_form_code(){ $ html=""; $ html.=''; $ html.=''; $ html.='
Họđệm*
'; $ html.='Tên*
'; $ html.='Địachỉ*
'; $ html.='电子邮件*
'; $ html.='Nội粪* dg
'; $ html.=''; $ html.=''; $ html.=''; $ html.=''; 返回$ html; } 和这里js(ly-script.js): (函数($){ $(document).ready(function(){ //执行AJAX表单提交 $('form.ly-form-ex').on('submit',function(e){ e.preventDefault(); $('#loading').html('loading ...'); var dataString={action:'contact_ajax'}; $ .ajax({ 输入:" POST", 网址:ly_form_ajax_object.ajaxurl, 数据:dataString, 成功:功能(数据){ $('#loading').html(data); }, 错误:函数(errorThrown){ 警报(错误抛出); } }); }); });//结束准备 })(jQuery);希望对您有帮助, 最好的
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 );Hope it is helpful for you, Best
-
- 2013-09-23
尝试添加
if
语句:function my_function(){ $id = $_POST['variation_id']; if(isset($_POST['variation_id'])) { //your coded function die(); } }// end function
Try adding an
if
statement:function my_function(){ $id = $_POST['variation_id']; if(isset($_POST['variation_id'])) { //your coded function die(); } }// end function
-
那将如何解决问题?注意接受的答案和原始代码.How would that solve the problem? Note the accepted answer and the original code.
- 1
- 2013-09-23
- fuxia
我对jQuery和AJAX特别陌生.我有个小问题,返回值始终为0,尽管我认为这实际上是成功消息,但不返回任何内容.
我已经搜索了Google-verse,并且在PHP回调中具有die()函数,并且我相信add_actions是正确的.
我正在本地主机上工作,尽管我怀疑这会影响本地主机,而这一切都在管理员而非前端中.我还检查了js是否已入队和本地化.
在chrome开发人员区域中,我收到200 OK消息.
我还从 http://codex.wordpress.org/AJAX_in_Plugins 中测试了基本AJAX,它还返回了0,这让我想知道它是否不是下面概述的代码.
现在,我只是想使它向jQuery发送一些信息.任何帮助将不胜感激.
jQuery
PHP