简体   繁体   中英

Angular 4 http post request shows data in the url

I try to create a login control in typescript. When i try to send data it's shown in url like get method.

Here's typescript code;

AdminLogin(login: any) {
    let loginJson = JSON.stringify({ login });
    let headers = new Headers({ "Content-Type": "application/json" });
    let options = new RequestOptions({ headers: headers });

    return this.http.post(this.linkAdminLogin, loginJson, options)
        .map((response: Response) => response.json())
        .catch(this._errorHandler);
}

And here's c# method

[HttpGet]
public JsonResult Login(string login)
{
    UserJson user = JsonConvert.DeserializeObject<UserJson>(login);

    string pass = user.Password.ToMD5();

    var rb = entity.Users.Where(a => a.Active == true && a.Username == user.Username && a.Password == pass).FirstOrDefault();

    if (rb != null)
    {
        Session["CurrentUser"] = rb;

        return Json(true, JsonRequestBehavior.AllowGet);
    }
    else
    {
        return Json(false, JsonRequestBehavior.AllowGet);
    }
}

When i go like this in the Chrome's network panel Login url is like http://localhost/AngularProject/Ajax/Home/Login?login=%7B%22Username%22:%22blahblah%22,%22Password%22:%22blahblah%22%7D

If i use HttpPost attribute it doesn't work. It gives an error like Http 404 Not found error for the method.

I just try to hide the data from url. I'm new in the Angular so i'm waiting for your answers.

Can you please try something like this. Angular example:

AdminLogin$(request: RequestModel): Observable<responseModel | type> {
  const url = '<url to api>';

  return this.http.post<responseModel | type>(url, {request})
    .pipe(catchError(this.errorHandler));

}

C# example:

[HttpPost]
public IActionResult Login([FromBody] RequestModel request)
{
  // your code
  return Ok();
}

尝试删除此行

   let loginJson = JSON.stringify({ login });

这里一样设置代理配置。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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