问题描述
我做了一个虚拟的django视图,接受 PUT
请求:
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()
现在,当我尝试对视图进行 PUT
xhr调用时,它返回403:
Now, when I try to make a PUT
xhr call to the view, it returns a 403:
(为什么它返回403,我现在不在乎)。真正的问题是:
(why it returns 403, I don't care right now). The REAL problem is this:
XHR调用返回状态0(已中止?!),即使实际响应为403,包含内容(通知内容长度为156400)。
The XHR call returns status 0 (aborted?!), even if the real response was a 403, with content (notice 156400 content length).
为什么不显示403响应?
编辑: PUT
请求如下:
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);
推荐答案
我认为浏览器假设它是一个交叉域请求收到403时意味着禁止访问。这就是浏览器没有将数据处理回发出请求的javascript代码的原因。服务器应为所有响应发送Action-Control-Allow-Origin标头。
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.
这篇关于为什么PUT 403显示为Aborted?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!