简体   繁体   English

离子:我的 http.post 到 WebApi RestFull 不起作用

[英]Ionic: My http.post to WebApi RestFull doesn't work

I'm developing an app with Ionic Framework and I'm changing some of the information with my web service API RestFull.我正在使用 Ionic Framework 开发一个应用程序,并且正在使用我的 Web 服务 API RestFull 更改一些信息。 When I connect with a GET method it works, but when I try to connect with a POST method it doesn't work.当我使用 GET 方法连接时它可以工作,但是当我尝试使用 POST 方法连接时它不起作用。

This is the code of Ionic:这是离子的代码:

import { HttpClient ] from '@angular/common/http';

constructor(private http: HttpClient ...)

And this is the function (I am returning a promise):这是函数(我正在返回一个承诺):

var user = {"nom" : "whatever", "password" : "whatever2"};
var options =  { headers: {'Content-Type': 'application/json; charset=utf-8;'}};

return this.http.post(myLink, user, options).toPromise();

In Web Service, the code is (C#):在 Web Service 中,代码为(C#):

[Route("AuthFullUser")]
[HttpPost]
public HttpResponseMessage login([FromBody]User user){
var u = UserRepository.login(user.nom.ToString(), user.password.ToString());
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, u);

return response;
}

If I try to enter from Ionic doesn't work, but with PostMan (an app) it works.如果我尝试从 Ionic 输入不起作用,但使用PostMan (一个应用程序)它可以工作。

On the other hand, if I put [HttpOptions] above the [Route("AuthFullUser")] I am able to access the web service but the parameters are NULL .另一方面,如果我将[HttpOptions]放在[Route("AuthFullUser") ]之上,我可以访问网络服务,但参数为NULL

I tried to make a request with $.ajax{} or $.post{} but neither worked.我尝试使用$.ajax{}$.post{}发出请求,但都没有奏效。

I tried to add on my headers:我试图添加我的标题:

"Acces-Control-Allow-Origin" : "*",
"Acces-Control-Allow-Credentials" : "true",
"Acces-Control-Allow-Methods" : "GET, POST, PUT, DELETE, OPTIONS",
"Acces-Control-Allow-Headers" : "Content-Type"

But neither worked.但都没有奏效。

I have been struggling for weeks on this.我为此苦苦挣扎了数周。 Any help would be greatly appreciated.任何帮助将不胜感激。

If someone is having the same trouble I found the solution.如果有人遇到同样的问题,我找到了解决方案。

It was all from the Web Service and his headers.这一切都来自 Web 服务和他的头文件。 You only need to allow all the headers, it's all on the documentation.您只需要允许所有标题,这一切都在文档中。

If someone have a question, they can put it here and i will try to give my best answer.如果有人有问题,他们可以把它放在这里,我会尽力给出我最好的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM