typescript 角2。网址。无法发送带有正文的帖子并设置 Content-Type
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42749192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular2. Http. Can't send post with body and set Content-Type
提问by Egorikas
I'm writing an app with using Angular2 as a front-end part(~2.4.0) and ASP.NET CORE as a back-end part.
我正在编写一个使用 Angular2 作为前端部分(~2.4.0)和 ASP.NET CORE 作为后端部分的应用程序。
I have a really huge problem with sending a post request. Chrome tells me that i have no body in request and also I can't set up content-type of request.
我在发送 post 请求时遇到了一个非常大的问题。Chrome 告诉我我没有请求正文,而且我无法设置请求的内容类型。
I tried in a few ways. But no one worked
我尝试了几种方法。但没有人工作
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('authentication', `3123123123`);
let options = new RequestOptions({ headers: headers });
this.http
.post('http://localhost:5000/api/user/login', JSON.stringify(this.loginModel), options)
.subscribe((result) => {
if (result) {
.......
}
});
Second:
第二:
PostRequest(url:string,data:any) {
var headers = new Headers();
headers.append("Content-Type", 'application/json');
var requestoptions = new RequestOptions({
method: RequestMethod.Post,
url: url,
headers: headers,
body: JSON.stringify(data)
})
return this.http.request(new Request(requestoptions))
.map((res: Response) =>
res.json()
).subscribe((result) => {
if (result) {
....
}
});
}
Third one:
第三个:
let headers = new Headers();
headers.append('Accept', 'application/json');
headers.append('Content-Type', 'application/json');
let body = JSON.stringify(loginDto);
let options = new RequestOptions({headers: headers});
return this.http
.post(
'....',
body,
options
)
.map(res => res.json()).subscribe((result) => {
if (result) {
....
}
});
UPD:
更新:
I've tried @Akshay Khale method and It still doesn't work. I still get headers with out content type
我已经尝试过@Akshay Khale 方法,但它仍然不起作用。我仍然得到没有内容类型的标题
RESOLUTION:
解决:
The problem was in a back-end side. CORS-configuration had been made in a wrong way. And ASP.NET CORE blocked all custom headers. Thank everyone for help
问题出在后端。CORS 配置以错误的方式进行。并且 ASP.NET CORE 阻止了所有自定义标头。谢谢大家的帮助
回答by Egorikas
回答by Akshay Khale
In your code first option should work but in the second parameter you won't need to do JSON.stringify
.
在您的代码中,第一个选项应该有效,但在第二个参数中您不需要执行JSON.stringify
.
Below is the Code that is working
以下是正在运行的代码
It will hit the given remote URL with Post request and console dump the returned data.
它将使用 Post 请求命中给定的远程 URL,并控制台转储返回的数据。
let body:any = { "parameter1": "value1", "parameter2": "value2" };
let url = "example.com";
let response:any;
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
this.http.post(url, body, options).map((res:Response) => res.json()).subscribe(
data => { response = data },
err => console.error(err),
() => { console.log(response) });
happy to help :) :) :)
很高兴能帮助你 :) :) :)
回答by Aravind
Since you are using Angular 4 RequestOptionsArgs
instead of RequestOptions
as below,
由于您使用的是 Angular 4RequestOptionsArgs
而不是RequestOptions
如下,
The official documentation for this is in Experiemental status
官方文档处于实验状态
post(url: string, body: any, options?: RequestOptionsArgs) : Observable<Response>
Modify your code as,
将您的代码修改为,
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('authentication', `3123123123`);
let options : RequestOptionsArgs ={
url: 'http://localhost:5000/api/user/login',
method: RequestMethod.Post,
search : '',
headers : headers,
withCredentials : 'false',
responseType : ResponseContentType.Json
}
this.http
.post('http://localhost:5000/api/user/login',
JSON.parse(this.loginModel), options)
.subscribe((result) => {
if (result) {
.......
}
});