400 bad request on admin-ajax.php only using wp_enqueue_scripts action hook
3 个回答
- 投票数
-
- 2018-01-17
我认为这里唯一缺少的是您需要将
add_action('wp_ajax_nopriv_ajaxlogin','ajax_login');
移到ajax_login_init
之外.该代码注册了您的Ajax处理程序,但是当您仅在
wp_enqueue_scripts
上运行它时,已经太迟了,并且wp_ajax_nopriv _
挂钩已经运行.所以,您尝试过类似的事情吗?
函数ajax_login_init(){ if(!is_user_logged_in()||!is_page('page-test')){ 返回; } wp_register_script('ajax-login-script',get_stylesheet_directory_uri().'/js/ajax-login-script.js',array('jquery')); wp_enqueue_script('ajax-login-script'); wp_localize_script('ajax-login-script','ajax_login_object',array('ajaxurl'=> admin_url('admin-ajax.php'),'redirecturl'=>'REDIRECT_URL_HERE','loadingmessage'=> __ ("正在发送用户信息,请稍候..."))); } add_action('wp_enqueue_scripts','ajax_login_init'); add_action('wp_ajax_nopriv_ajaxlogin','ajax_login'); 函数ajax_login(){ //nonce-field在页面上创建 check_ajax_referer('ajax-login-nonce','security'); //码 死(); }
编辑:
现在,很显然,您只想在该特定页面上加载JavaScript.这意味着您需要将
is_page()
放在ajax_login_init()
中.我已经相应地更新了代码.现在,为什么您的解决方案不起作用?
is_page()
检查意味着您的功能文件仅在该特定页面上加载.ajax_login_init()
被调用,您的脚本进入队列.到目前为止一切顺利.现在,您的脚本将进行ajax调用.如评论中所述,ajax调用不知道您当前所在的页面.该文件位于
wp-admin/admin-ajax.php
上是有原因的.没有WP_Query
,因此is_page()
在ajax请求期间不起作用.由于不起作用,因此
sw18_page_specific_functions()
在ajax上下文中将不会执行任何操作.这意味着您的功能文件尚未加载,并且ajax处理程序不存在.这就是为什么您需要始终包含该功能文件并在
ajax_login_init()
中移动is_page()
进行检查的原因.因此,直接运行
include_once dirname(__ FILE__).而不是
.没有任何sw18_page_specific_functions(){…}
.'/includes/my-page-test-functions.php';add_action('parse_query')
调用.I think the only thing missing here is that you need to move
add_action('wp_ajax_nopriv_ajaxlogin','ajax_login');
outsideajax_login_init
.That code registers your Ajax handler, but when you only run it on
wp_enqueue_scripts
, it's already too late andwp_ajax_nopriv_
hooks are already run.So, have you tried something like this:
function ajax_login_init(){ if ( ! is_user_logged_in() || ! is_page( 'page-test' ) ) { return; } wp_register_script('ajax-login-script',get_stylesheet_directory_uri().'/js/ajax-login-script.js',array('jquery')); wp_enqueue_script('ajax-login-script'); wp_localize_script('ajax-login-script','ajax_login_object',array('ajaxurl' => admin_url('admin-ajax.php'),'redirecturl' => 'REDIRECT_URL_HERE','loadingmessage' => __('Sending user info, please wait...'))); } add_action( 'wp_enqueue_scripts','ajax_login_init' ); add_action( 'wp_ajax_nopriv_ajaxlogin','ajax_login' ); function ajax_login(){ //nonce-field is created on page check_ajax_referer('ajax-login-nonce','security'); //CODE die(); }
Edit:
Now it's more clear that you only want to load the JavaScript on that particular page. This means you need to put your
is_page()
insideajax_login_init()
. I've updated the code accordingly.Now, why didn't your solution work?
The
is_page()
check meant that your functions file was only loaded on that specific page.ajax_login_init()
gets called and your scripts enqueued. So far so good.Now your script makes the ajax call. As mentioned in the comments, ajax calls are not aware of the current page you're on. There's a reason the file sits at
wp-admin/admin-ajax.php
. There's noWP_Query
and thusis_page()
does not work during an ajax request.Since that does not work,
sw18_page_specific_functions()
won't do anything in an ajax context. This means your functions file is not loaded and your ajax handler does not exist.That's why you need to always include that functions file and move that
is_page()
check insideajax_login_init()
.So instead of
sw18_page_specific_functions() { … }
just runinclude_once dirname(__FILE__).'/includes/my-page-test-functions.php';
directly. Without anyadd_action( 'parse_query' )
call.-
好建议.我已经更改了该错误(仍然是相同的错误),但是问题仍然是包含函数的文件加载得太晚了.但是我需要一种方法来区分使用哪个页面.-目前,如上所述,我使用is_page()尝试了此操作.Good suggestion. I have changed that (still the same error), but the problem still is that the file containing the functions will load too late. But I need a way to distinguish which page is used. - currently I try this with is_page () as described above.
- 0
- 2018-01-17
- Sin
-
您是要从ajax_login()内部还是从ajax_login_init()内部运行is_page().前者无法工作,因为它处于Ajax上下文中.Are you trying to run `is_page()` from within `ajax_login()` or from within `ajax_login_init()`. The former can't work because it's in an Ajax context.
- 0
- 2018-01-17
- swissspidy
-
正如上面的描述性文字,我已经列举了函数所在的文件.is_page()在functions.php中使用,仅在需要时用于将文件包含在ajax函数中.I have enumerated the files in which the functions are, as descriptive text above. The is_page() is used in the functions.php and serves to include the file with the ajax functions only when needed.
- 0
- 2018-01-17
- Sin
-
@Sin同样,`is_page()`在Ajax上下文中不起作用.我已经相应更新了我的答案.@Sin Again, `is_page()` does not work in an Ajax context. I have updated my answer accordingly.
- 0
- 2018-01-17
- swissspidy
-
- 2019-04-14
请记住将"操作"功能名称附加到
wp_ajax_
标记上.function fetchorderrows() { // Want to run this func on ajax submit // Do awesome things here all day long } add_action('wp_ajax_fetchorderrows', 'fetchorderrows', 0);
Remember to have the 'action' function name appended to the
wp_ajax_
tag.function fetchorderrows() { // Want to run this func on ajax submit // Do awesome things here all day long } add_action('wp_ajax_fetchorderrows', 'fetchorderrows', 0);
-
-
嗨Zee Xhan.欢迎来到该网站.您的答案需要一些修改.首先,如果您的答案是代码,请不要发布屏幕截图.而是将代码作为摘要发布,并将其格式化为代码(使用{}按钮).这可能是您的答案被否决并且未被接受的原因.另外,多一点解释将是有帮助的-就像"为什么"只写die(),这相对于OP中的代码到底在哪里(原始文章)?Hi Zee Xhan. Welcome to the site. Your answer needs some revisions. First, if your answer is code, don't post a screenshot. Instead, post the code as a snippet and format it as code (use the {} button). That's likely the reason your answer was downvoted and not accepted. Also, a little more explanation would be helpful - like "why" just write die(), and where exactly does this go in relation to the code in the OP (original post)?
- 9
- 2018-11-12
- butlerblog
-
我最近一直在研究ajax.您在网上找到的教程都非常相似并且很容易实现. 但是我总是在我的
ajax-admin.php
文件中收到一个错误请求400 .经过长时间深入的搜索,我现在发现这是由于集成时间所致.
如果我使用
init
动作钩子初始化脚本和wp_localize_script
,则一切正常.因此,代码本身必须正确.my-page-test-functions.php
但是如果我使用例如
wp_enqeue_scripts
动作挂钩我总是收到错误的请求.与此有关的问题是:
我想在一个额外的php文件中包含这些功能,并仅在特定页面上需要它们时才加载它们.为此,我需要例如
is_page()
. 但是,当我将包含包含到parse_query
操作钩子中的函数挂钩时,is_page()
最早可以工作:functions.php
所以我想,钩在
my-page-test-functions.php
文件中的init
钩子上的函数不会触发,因为init
在parse_query
之前.是否有最佳实践来组织此操作,所以它可行?还是在使用
wp_enqeue_scripts
操作挂钩时如何解决admin-ajax.php
错误请求?