casolorz
casolorz

Reputation: 9544

JQuery ajax call on same domain not sending cookies when data is sent

I am trying to test whether we can convert our existing file upload form into a more ajax/html5 without changing much of the backend. So here is what I am doing on the test:

function sendFile() {
            var data = new FormData();
            var file = $('input[type=file]').get(0).files[0];
            data.append("file1",file);
            $.ajax({
                type: 'post',
                url: '/upload.jsp',
                data: data,
                contentType: 'multipart/form-data',
                processData: false
            });
}

So the file sending part is not the problem. The problem is that I am not getting any of my cookies. If I take the data: data param out then it sends the cookies just fine.

Any idea why?

Thanks.

Upvotes: 2

Views: 415

Answers (1)

balrok
balrok

Reputation: 454

You need to set the withCredentials $.ajax() setting:

function sendFile() {
    var data = new FormData();
    var file = $('input[type=file]').get(0).files[0];
    data.append("file1",file);
    $.ajax({
        type: 'post',
        url: '/upload.jsp',
        data: data,
        contentType: 'multipart/form-data',
        processData: false,
        xhrFields: {
           withCredentials: true
        }
    });
}

Upvotes: 1

Related Questions