Codeigniter在ajax调用上给出了404错误

时间:2013-11-30 16:52:47

标签: javascript php jquery ajax codeigniter

我有一个上传表单,在ajax上传后2个按钮允许用户更改上传的照片或删除它。我已经实现了整个系统并且工作正常。但我的问题是,当我试图对删除它给我csrf protection error的照片的函数进行ajax调用时。在我停用csrf protection后,现在我在回复中说404 not found

PHP: //网址:“http://localhost/project/index.php/ajax/deleteCarPhoto/

function deleteCarPhoto()
{
    $src = $this->input->post('src');

    $adPhotos = $this->session->userdata('ad_photos');
    unset($adPhotos[$src]);

    $this->session->set_userdata('ad_photos',$adPhotos);

    echo "<p>Photo deleted</p>";
}

JS:

$("label.btn-danger").on('click',function(e){
    e.preventDefault();
    var selectedElement = this;
    var name = $(this).parents("label").find("input[type=file]").attr('name');
    var src = $(this).parents("label").find("img").attr("src");
    var pattern = /\/([A-Z0-9_-]{1,}\.(?:png|jpg|gif|jpeg))/ig;
    var result = pattern.exec(src)[1];

    $.post({
            url: "http://localhost/project/index.php/ajax/deleteCarPhoto/",
            data: {src : result},
            dataType:'html'
    },function(xhr){
        alert(xhr.responseText);
        $(selectedElement).parents('label').find('.fileinput-preview').css('background',"none");
        $(selectedElement).parents('label').find('.fileinput-preview').css('background',"url('http://localhost/project/assets/images/upload_a_photo.png') no-repeat center center");
        $(selectedElement).parents('label').find('input[type=file]').removeAttr('disabled');
        $("#upload").reset();
    });
});

我的HTTP标头:

Accept  */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Cache-Control   no-cache
Connection  keep-alive
Content-Length  0
Cookie  ZDEDebuggerPresent=php,phtml,php3; un4vW7pAFT=aNdO76M9erJW9lpHvJtS9SILUiZqb%2FgYGb5Zr9liqlVP3hzLupNYgeWlRAeGOME6mK4xE7ATNdmr%2FVSLdkFsyMX4foVtEzmxOsT%2BX9N9K%2FdqDMTaL0pYlvxloe5zYHYBXHanmSbMhWhPvcZO65HGb29VZsnquPuR%2BVwAnyFsd3R7l0s7TjutaydBdcgUvRUv1n1FkqRZ5oTQ2JfW8RGo7Pye4Lb4VA8OLyzVTgmroVkr%2FA3g24y82EWHAPyv%2B4riDiYmmxVKDWmWdMZ7Ms4sqibYVw4woUyXtNxoWvD6vSphcpw9oPCIsm2guCyf3hJlaIuVvdM0sRvxPf3tz0zvIA%3D%3D
Host    localhost
Pragma  no-cache
Referer http://localhost/project/index.php/advertise/add
User-Agent  Mozilla/5.0 (Windows NT 6.1; rv:25.0) Gecko/20100101 Firefox/25.0
X-Requested-With    XMLHttpRequest

1 个答案:

答案 0 :(得分:2)

如果我是浏览器,那么如果我的电脑是localhost。如果我正在浏览您的网站http://www.your-web-domain/some/url,那么指向localhost的ajax链接仍会引用我的工作站。脚本中的链接应为http://www.your-web-domain//project/index.php/ajax/deleteCarPhoto/