【发布时间】:2013-09-02 21:33:36
【问题描述】:
我只想将以下 JSONobjects 发送到我的 API 后端:
{
"username":"alex",
"password":"password"
}
所以我编写了以下函数,使用 Angular $http:
$http(
{
method: 'POST',
url: '/api/user/auth/',
data: '{"username":"alex", "password":"alex"}',
})
.success(function(data, status, headers, config) {
// Do Stuff
})
.error(function(data, status, headers, config) {
// Do Stuff
});
我在 POST 方法的文档中读到 Content-Type 标头将自动设置为 "application/json"。
但我意识到我在后端 (Django+Tastypie) api 上收到的内容类型是 "text/plain"。
这会导致我的 API 无法正确响应此请求。我应该如何管理这种内容类型?
【问题讨论】:
-
您的后端如何检索详细信息?
-
我使用 Django Tastypie 作为后端。我在 $http 发送的内容类型中看到 text/plain。 raw_post_data 或 POST 数据也是空的。
-
太奇怪了...如果我放标题:{'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'} 它正在工作.. 但是如果我把 application/json... 它不是...
-
我也看到了这种行为。当数据块为空时,Angular 似乎忽略了默认的“application/json”并将 Content-Type 设置为“plain/text”。在 $http 上显式设置默认值或将 Content-Type 设置为 put() 调用的参数似乎都没有任何效果。我正在使用 AngularJS 1.2.0。
-
您是否通过配置阶段设置标题?
标签: javascript django angularjs tastypie