【发布时间】:2016-01-22 14:33:37
【问题描述】:
我打算将 url 作为 GET 参数传递给 restful API,但它无法匹配 url 模式,因此返回 404。
我使用 django-rest 框架作为服务器端。
这里是urls.py
url(r'^method/?P<url>(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*))$',views.Method.as_view()), name='method'),
而客户端的xhr代码是这样的。
var xhr = new XMLHttpRequest();
// url is like https://storage.googleapis.com/xxx/cccc/abc.txt
xhr.open('GET', 'method/' + url, true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e) {
if (this.status == 200) {
// do something
}
};
xhr.send();
正则表达式正常,但服务器仍返回 404。
即使我传递了类似的请求网址
xhr.open('GET', 'method?=' + url, true);
它仍然返回 404 not found.
这样做的正确方法是什么?
谢谢!
【问题讨论】:
-
get 参数中类似 url 的值应该是 url 编码的.. 可能这就是为什么.. 试试..
标签: ajax django rest xmlhttprequest django-rest-framework