Angular2 ES5 - 在 HTTP POST header 中覆盖 Content-Type
Angular2 ES5 - overriding Content-Type in HTTP POST header
正在尝试覆盖 header 的内容类型,但它仍然出现在 text/plain。使用 Ben Nadel 的 GateWayAPI 有一种方法可以做到这一点,但希望有一种不涉及自定义包装的解决方案。
var url = this.url;
var body = JSON.stringify({email_login: "login", password_login: "password"});
var headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
return this.http.post(url, body, {headers:headers})
.map(function (response) {
return response.json();
})
.catch(this.handleError);
改用追加函数如何:
let headers = new Headers();
headers.append("Content-Type", "application/x-www-form-urlencoded");
Http
的请求 post
具有以下签名:
post(url: string, body: any, options?: RequestOptionsArgs)
第三个参数是 RequestOptionsArgs
类型的参数,这意味着您必须将 RequestOptions
传递给它,而不是 Headers
。您还应该使用箭头运算符 (=>
) 而不是 function
。您正在寻找的是这样的东西:
result: any;
httpPostTest() {
var url = this.url;
var body = JSON.stringify({email_login: "login", password_login: "password"});
let headers: Headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded' });
let options: RequestOptions = new RequestOptions({ headers: headers });
this.http.post(url, body, options)
.map(response => {
response = response.json();
})
.catch(this.handleError);
}
这之后需要用subscribe来获取response
,所以假设这个函数叫做httpPostTest
(我在上面加上),我们想把response存储在变量中称为 result
:
this.httpPostTest().subscribe(
response => { this.result = response; },
error => { console.log('ERROR'); },
() => { console.log('FINISHED')}
);
您应该创建 RequestOptions class 的实例并使用字符串而不是 json 对象,因为您正在使用 form-urlencoded:
var body = "email_login=login&password_login=password";
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let options = new RequestOptions({ headers: headers });
this.http.post(url, body, options);
正在尝试覆盖 header 的内容类型,但它仍然出现在 text/plain。使用 Ben Nadel 的 GateWayAPI 有一种方法可以做到这一点,但希望有一种不涉及自定义包装的解决方案。
var url = this.url;
var body = JSON.stringify({email_login: "login", password_login: "password"});
var headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
return this.http.post(url, body, {headers:headers})
.map(function (response) {
return response.json();
})
.catch(this.handleError);
改用追加函数如何:
let headers = new Headers();
headers.append("Content-Type", "application/x-www-form-urlencoded");
Http
的请求 post
具有以下签名:
post(url: string, body: any, options?: RequestOptionsArgs)
第三个参数是 RequestOptionsArgs
类型的参数,这意味着您必须将 RequestOptions
传递给它,而不是 Headers
。您还应该使用箭头运算符 (=>
) 而不是 function
。您正在寻找的是这样的东西:
result: any;
httpPostTest() {
var url = this.url;
var body = JSON.stringify({email_login: "login", password_login: "password"});
let headers: Headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded' });
let options: RequestOptions = new RequestOptions({ headers: headers });
this.http.post(url, body, options)
.map(response => {
response = response.json();
})
.catch(this.handleError);
}
这之后需要用subscribe来获取response
,所以假设这个函数叫做httpPostTest
(我在上面加上),我们想把response存储在变量中称为 result
:
this.httpPostTest().subscribe(
response => { this.result = response; },
error => { console.log('ERROR'); },
() => { console.log('FINISHED')}
);
您应该创建 RequestOptions class 的实例并使用字符串而不是 json 对象,因为您正在使用 form-urlencoded:
var body = "email_login=login&password_login=password";
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let options = new RequestOptions({ headers: headers });
this.http.post(url, body, options);