ajax message(,jquery - Ajax success message returns HTML of my homepage - Stack Overflow

博主分享了自己在尝试进行Ajax请求时遇到的问题,由于未登录导致请求返回整个主页,而非预期结果。通过检查代码,发现是由于阻止了未授权用户访问特定功能文件引起的。解决方案是使用`wp_ajax_nopriv`来允许未登录用户进行Ajax请求。此外,博客还提醒读者注意在处理权限问题时要留意不寻常的响应,以防被误导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I figured it out...

This might help some newbie like myself who makes the same mistake so I'll answer my own question :)

Turns out I wasn't logged in, go figure. The response was strange though and helped to throw me off, returning the entire homepage. The reason for this is I had blocked my functions files which includes the Ajax responder like so:

function block_users()

{

if( !current_user_can( 'delete_pages' ) ) {

wp_redirect( get_home_url(), 301 );

exit;

}

}

add_action('admin_init','block_users');

I should have known to log in (thought I was) but, such unexpected results and it was friday :)

Also note to anyone who stumbles onto this: if you do want someone to do ajax without being logged in use the no-priviledge version of wp_ajax, wp_ajax_nopriv.

add_action( 'wp_ajax_nopriv_action', 'function' );

In lieu of

add_action( 'wp_ajax_action', 'function' );

The former does not require permission, ie being logged in, to do ajax requests.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值