简体   繁体   English

为什么发出ajax请求时显示403(禁止)?

[英]Why it shows 403(forbidden) when making an ajax request?

I've made an ajax call with this: 我用这个进行了ajax调用:

$('.start-rate-fixed').on('click', function(e){
        e.preventDefault();
        var videoRate = $('.start-rate input[name="rating"]:checked').val(),
            productId = parseInt($('.popover-content').prop('id'));
        $.ajax({
            url : ROOT + 'products/rate_video',
            type : 'POST',
            data : {
                'data[Product][id]' : productId,
                'data[Product][success_rate]' : videoRate
            }
        }).done(function(res){
            var data = $.parseJSON(res);
            alert(data);
        });
    });

Where I defined ROOT as the webroot of my cakephp project in my default.ctp with this: 我在default.ctp中用以下命令将ROOT定义为cakephp项目的webroot:

<script type="text/javascript">
    var ROOT = '<?php echo $this->Html->url('/');?>';
</script>

and trying to retrieve data from a function "rate_video" defined in my products controller but I get this error. 并尝试从产品控制器中定义的“ rate_video”函数中检索数据,但出现此错误。 Also I've tried a simple ajax for a test function but it showed me the same issue. 我也为测试功能尝试了一个简单的ajax,但是它向我展示了同样的问题。

Controller Code 控制器代码

public function rate_video(){
        $this->autoRender = false;
        if($this->request->is('post') && $this->request->is('ajax')){
            $success_rate = $this->request->data['Product']['success_rate'];
            $this->Product->id = $this->request->data['Product']['id'];
            if($this->Product->saveField('success_rate', $success_rate)){
                echo json_encode('Successfully Rated');
            } else {
                echo json_encode('Error!!');
            }
        }
    }

Please add dataType and a forward slash (/) at the end of your request URL 请在请求网址的末尾添加dataType和正斜杠(/)

$.ajax({
        url : ROOT + 'products/rate_video/',
        type : 'POST',
        data : {
            'data[Product][id]' : productId,
            'data[Product][success_rate]' : videoRate
        },
        dataType: 'json',
    }).done(function(res){

I just had the same problem and solved it by putting the URL within AJAX call to a URL that I know works. 我只是遇到了同样的问题,并通过将AJAX调用中的URL放入我知道有效的URL来解决了该问题。 Then try accessing the URL that you are trying to invoke via AJAX directly within the web browser - most likely you are accessing a controller that does not have a view file created. 然后,尝试直接在网络浏览器中访问要通过AJAX调用的URL-最有可能是您访问的控制器没有创建视图文件。 To fix this you have to ensure that the controller method being accessed does not have a view to be rendered - set $this->render(null) 要解决此问题,您必须确保所访问的控制器方法没有要呈现的视图-设置$ this-> render(null)

  1. If you have incorrect url then 如果您的网址不正确,则

     url: '<?php echo Router::url(array('controller' => 'Controllername', 'action' => 'actionname')); ?>' 

    this above url provide ajax to url from root to your action. 上面的网址提供了从根目录到您操作的ajax网址。

  2. And other cause for 403 is your auth function, if your using auth in your controller then make your ajax function allow like 导致403的其他原因是您的auth函数,如果您在控制器中使用auth,则使ajax函数允许

     $this->Auth->allow('Your ajax function name here'); 

Your script placed at localhost/dev.popover/products/rate_video but ajax ROOT is / - that mean localhost/ and ajax sent request to 您的脚本位于localhost/dev.popover/products/rate_video但是ajax根目录为/ -表示localhost/和ajax将请求发送至

'localhost/products/rate_video'

Right solution is 正确的解决方案是

<script type="text/javascript">
    var ROOT = '<?php echo $this->Html->url('/dev.popover/');?>';
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM