简体   繁体   中英

Codeigniter gives 404 error on ajax call

I have an upload form which after ajax upload 2 buttons allow the user to change the uploaded photo or delete it. I've implemented the whole system right and works fine. but my problem is and was when I tried to make an ajax call to the function that deletes photos it gave me csrf protection error . after I disabled csrf protection , now it says 404 not found in my response.

PHP: //url: " 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();
    });
});

My HTTP HEADERS:

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

If I am a browser, then localhost if my computer. If I am browsing your site http://www.your-web-domain/some/url , then an ajax link to localhost will still refer to my workstation. The link in the script should be http://www.your-web-domain//project/index.php/ajax/deleteCarPhoto/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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