CodeIgniter控制器上的Ajax调用导致403 Forbidden [英] Ajax call on CodeIgniter controller results in 403 Forbidden

查看:116
本文介绍了CodeIgniter控制器上的Ajax调用导致403 Forbidden的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ajax进行CodeIgniter 3.x开发.我有一个可以从浏览器访问的控制器/方法.但是,当我尝试使用Ajax请求/调用该页面时,似乎总是会导致403 Forbidden-error.

I am using CodeIgniter 3.x development with Ajax. I have a controller/method which is accessible from my browser. However, when I try to request/call that page with Ajax it seems to always result in a 403 Forbidden-error.

我搜索了各种搜索引擎(包括StackOverflow),这些搜索引擎都出现了一些类似的问题.他们中没有一个人对我的问题有一个明确的解决方案,并且我对此已经坚持了很长时间.我可以使用CI与Ajax请求一起访问不会加载的页面.我的CI配置似乎有问题.

I have searched on various search engines (incl. StackOverflow) which all showed up some similar problems. None of them had a clear solution to my problem and I'm stuck which this for a long time now. I can access pages that do not load using CI with an Ajax-request just fine. It seems to be some problem with my CI configuration.

我首先想到了一些与会话相关的问题,但是在完全用CI切换会话后,它却无法正常工作.下面是我用来进行Ajax请求的一些代码,我认为这很好,应该可以工作(就像在非CI页面上一样).

I first thought of some session-related problem, but after turning of sessions completely in CI it did not work however. Below is some of my code I'm using to make the Ajax-request, which in my opinion is just fine and should work (as it does on non CI-pages).

$.ajax({
    url : data_url,
    type: 'POST',
    data: pay_load,
    async: false,
    success: function(data) {
        result = JSON.parse(data);
    }
});

推荐答案

您的ajax data 必须包括隐藏的CSRF令牌字段的值.这是通过标准表单操作提交的部分,但是在执行ajax时会丢失.

Your ajax data must include the value of the hidden CSRF token field. This is the part that gets submitted with the standard form action but is missing when you do the ajax.

这篇关于CodeIgniter控制器上的Ajax调用导致403 Forbidden的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆