Javascript Angular 4 Http POST 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46711946/
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
Angular 4 Http POST not working
提问by Osama Sheikh
I hope everyone is doing great. I've recently started working with angular 4.4, i've been trying to post data to my api server, but unfortunately it's not working. I've spent like 2 days on it but still no success. And have already tried 6-7 article even from angular.io. I've tried both Httpand Httpclientmodules but nothing seems to be working.
我希望每个人都做得很好。我最近开始使用 angular 4.4,我一直在尝试将数据发布到我的 api 服务器,但不幸的是它不起作用。我已经花了大约 2 天的时间,但仍然没有成功。甚至已经从angular.io尝试了 6-7 篇文章。我已经尝试了Http和Httpclient模块,但似乎没有任何效果。
The problem is, whenever i try to post data to my server, Angular makes http OPTIONStype request instead of POST.
问题是,每当我尝试将数据发布到我的服务器时,Angular 都会发出 http OPTIONS类型的请求而不是 POST。
this.http.post('http://myapiserver.com', {email: '[email protected]'}).subscribe(
res => {
const response = res.text();
}
);
And i've also tried to send custom options with the request but still no success.
而且我还尝试通过请求发送自定义选项,但仍然没有成功。
const headers = new Headers({ 'Content-Type': 'x-www-form-urlencoded' });
const options = new RequestOptions({ headers: headers });
options.method = RequestMethod.Post;
options.body = {name: 'Adam Smith'};
//options.body = JSON.stringify({name: 'Adam Smith'}); // i also tried this
//options.body = 'param1=something¶m2=somethingelse'; // i also tried this
I was using ASP.NET core 2.0, but since it wasn't working i also tried simple php code, Here is the server side code for php. And it's also not working.
我使用的是 ASP.NET core 2.0,但由于它不起作用,我也尝试了简单的 php 代码,这是 php 的服务器端代码。它也不起作用。
<?php
print_r($_POST);
?>
Note: Cors are also enabled on server. Plus I also tried simple get request and its working perfectly fine.
注意:服务器上也启用了 Cors。另外,我还尝试了简单的 get 请求,并且它工作得很好。
I'll really appreciate some help.
我真的很感激一些帮助。
Thanks in advance
提前致谢
回答by Osama Sheikh
I solved it by setting the Content-Type to application/x-www-form-urlencoded:
我通过将 Content-Type 设置为application/x-www-form-urlencoded:
const headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
const options = new RequestOptions({ headers: headers });
const params = new URLSearchParams();
params.append('mypostField', 'myFieldValue');
http.post('myapiendpoint.com', params, options).subscribe();
回答by JayDeeEss
have you tried passing headers as the third argument in the post menthod:
您是否尝试过将标题作为 post 方法中的第三个参数传递:
this.http.post('http://myapiserver.com', JSON.stringify({name: 'Adam Smith'}), { headers: new Headers({ 'Content-Type': 'application/json' }) }).subscribe(
res => {
const response = res.text();
}
);
make sure you import Headers from @angular/http
确保从@angular/http 导入标题
回答by Reshan Pubudu
I had same problem and i used like this.(using FormData) try it. It work for me.
我有同样的问题,我是这样使用的。(使用 FormData)试试吧。它对我有用。
let formdata = new FormData();
formdata.append('email', '[email protected]');
this.http.post('http://myapiserver.com', formdata).subscribe(
res => {
const response = res.text();
}
);
回答by Ferdys Duran
import { HttpClient, HttpHeaders } from '@angular/common/http'; import { Observable } from 'rxjs';
从“@angular/common/http”导入 { HttpClient, HttpHeaders }; 从 'rxjs' 导入 { Observable };
saveGame(route,game):Observable<any>{
let json = JSON.stringify(game);
let params = 'json=' + json;
let headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded');
return this.http.post(this.URL + route, params, { headers: headers });
}

