【问题标题】:Pass a url as a parameter with the GET methoid in the django-rest framework?使用 django-rest 框架中的 GET 方法将 url 作为参数传递?
【发布时间】: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


【解决方案1】:

更改您的客户端 xhr 代码,例如:

var xhr = new XMLHttpRequest();
var url = 'https://storage.googleapis.com/xxx/cccc/abc.txt';
xhr.open('GET', 'method/?url=' + encodeURIComponent(url), true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e) {
    if (this.status == 200) {
        // do something
    }
};
xhr.send();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    • 2011-12-19
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    相关资源
    最近更新 更多