使用$ http发送JSON导致angular发送text/plain内容类型

Ale*_*Grs 6 javascript django tastypie angularjs

我只想将以下JSONobjects发送到我的API后端:

{
    "username":"alex",
    "password":"password"
}
Run Code Online (Sandbox Code Playgroud)

所以我使用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
});
Run Code Online (Sandbox Code Playgroud)

我在POST文档的文档中读到Content-Type标题将自动设置为"application/json".

但我意识到我在后端(Django + Tastypie)api上收到的内容类型是"text/plain".

这导致我的API无法正确响应此请求.我该如何管理这种内容类型?

BKM*_*BKM 0

尝试这个;

$http.defaults.headers.post["Content-Type"] = "application/json";

$http.post('/api/user/auth/', data).success(function(data, status, headers, config) {
 // Do Stuff
})
.error(function(data, status, headers, config) {
 // Do Stuff
});
Run Code Online (Sandbox Code Playgroud)