[英]Angular2/Http (POST) headers
I'm unable to change the headers when doing a POST request. 在执行POST请求时我无法更改标头。 I tried a couple of things: 我尝试了几件事:
Simple class: 简单的课程:
export class HttpService {
constructor(http: Http) {
this._http = http;
}
}
I tried: 我试过了:
testCall() {
let body = JSON.stringify(
{ "username": "test", "password": "abc123" }
)
let headers = new Headers();
headers.append('Content-Type', 'application/json'); // also tried other types to test if its working with other types, but no luck
this._http.post('http://mybackend.local/api/auth', body, {
headers: headers
})
.subscribe(
data => { console.log(data); },
err => { console.log(err); },
{} => { console.log('complete'); }
);
}
2: 2:
testCall() {
let body = JSON.stringify(
{ "username": "test", "password": "abc123" }
)
let headers = new Headers();
headers.append('Content-Type', 'application/json'); // also tried other types to test if its working with other types, but no luck
let options = new RequestOptions({
headers: headers
});
this._http.post('http://mybackend.local/api/auth', body, options)
.subscribe(
data => { console.log(data); },
err => { console.log(err); },
{} => { console.log('complete'); }
);
}
none of the two are working. 这两个人都没有工作。 I didn't forget to import any of the classes. 我没有忘记导入任何类。
I'm using Google Chrome. 我正在使用Google Chrome。 So I check the 'Network' tab, my request is there, and it says my Content-Type is text/plain. 所以我检查了“网络”标签,我的请求就在那里,它说我的Content-Type是text / plain。
Is this a bug or am I doing something wrong? 这是一个错误还是我做错了什么?
UPDATE I forgot to import the Headers class from Angular2/http: 更新我忘了从Angular2 / http导入Headers类:
import {Headers} from 'angular2/http';
I think you're using the HTTP support of Angular2 the right way. 我认为你正在以正确的方式使用Angular2的HTTP支持。 See this working plunkr: https://plnkr.co/edit/Y777Dup3VnxHjrGSbsr3?p=preview . 请参阅此工作plunkr: https ://plnkr.co/edit/Y777Dup3VnxHjrGSbsr3 ? p = preview。
Perhaps, you forgot to import the Headers
class. 也许,你忘了导入Headers
类。 I made this mistake some time ago and there was no error in the JavaScript console but the headers I tried to set weren't actually set. 我前一段时间犯了这个错误,JavaScript控制台没有错误,但我试图设置的标题实际上没有设置。 For example regarding the Content-Type
header I had text/plain
instead of application/json
. 例如,关于Content-Type
标头,我有text/plain
而不是application/json
。 You can reproduce this in the plunkr I provided to you by commenting Headers
in the imports... 您可以通过在导入中评论Headers
在我提供给您的plunkr中重现这一点...
Here is a complete working sample (imports included): 这是一个完整的工作样本(包括导入):
import {Component} from 'angular2/core';
import {Http,Headers} from 'angular2/http';
import 'rxjs/Rx';
@Component({
selector: 'my-app',
template: `
<div (click)="executeHttp()">
Execute HTTP
</div>
`
})
export class AppComponent {
constructor(private http:Http) {
}
createAuthorizationHeader(headers:Headers) {
headers.append('Authorization', 'Basic ' +
btoa('a20e6aca-ee83-44bc-8033-b41f3078c2b6:c199f9c8-0548-4be79655-7ef7d7bf9d20'));
}
executeHttp() {
var headers = new Headers();
this.createAuthorizationHeader(headers);
headers.append('Content-Type', 'application/json');
var content = JSON.stringify({
name: 'my name'
});
return this.http.post(
'https://angular2.apispark.net/v1/companies/', content, {
headers: headers
}).map(res => res.json()).subscribe(
data => { console.log(data); },
err => { console.log(err); }
);
}
}
Hope it helps you, Thierry 希望它对你有帮助,蒂埃里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.