Why does a PUT 403 show up as Aborted?

2019-06-09 16:37发布

I've made a dummy django view that accepts PUT requests:

# urls.py
url(r'^put/.*$', 'put', name='put'),

# views.py
def put(request):
    print request.method
    return HttpResponse()

Now, when I try to make a PUT xhr call to the view, it returns a 403:

[27/Sep/2012 22:32:43] "PUT /put/x-unconverted/e02ed7da08d411e2bfa974de2b4d1b84?partNumber=115&uploadId=35UxOsGCCG98rke3VjpazmCy.0ZFpesndJ.XPp5Bw6R2CumfIsYKP5DlBYPY3gh3I0PCwfCg4DqSRttYp75bZg-- HTTP/1.1" 403 156400

(why it returns 403, I don't care right now). The REAL problem is this:

enter image description here

The XHR call returns status 0 (aborted?!), even if the real response was a 403, with content (notice 156400 content length).

Why doesn't it show the 403 response?

EDIT: the PUT request is made like this:

var xhr = new XMLHttpRequest();
var path = "/" + u.settings.key;
path += "?partNumber=" + (chunk + 1) + "&uploadId=" + u.upload_id;

var method = "PUT";
var authorization = "AWS " + u.settings.access_key + ":" + signature;
var blob = u.file.slice(start, end); // mozSlice / webkitSlice, depending on browser
xhr.upload.addEventListener("progress", progress_handler);
xhr.addEventListener("readystatechange", handler);
xhr.addEventListener("error", error_handler);
xhr.addEventListener("timeout", error_handler);

xhr.open(method, /*u.settings.host*/ "http://localhost:8000/put" + path, true);

xhr.setRequestHeader("x-amz-date", date);
xhr.setRequestHeader("Authorization", authorization);
xhr.setRequestHeader("Content-Type", u.settings.content_type);
xhr.setRequestHeader("Content-Disposition", "attachment; filename=" + u.file.name);

xhr.send(blob);

1条回答
放荡不羁爱自由
2楼-- · 2019-06-09 17:25

I think browser is assuming that it is a cross-domain request when it receives 403 which means forbidden access. That's the reason why browser is not handling data back to the javascript code that made the request. Server should send 'Action-Control-Allow-Origin' header for all the responses.

查看更多
登录 后发表回答