Angular4 - post form data to rest api

后端 未结 4 1167
星月不相逢
星月不相逢 2020-12-23 23:50

How is it possible to post form data to an external rest api?

At the moment i have an html form:

4条回答
  •  生来不讨喜
    2020-12-24 00:24

    Ok, so it turns out i have to add .subscribe() to post for it to do something. Also if i put "user" straight into post request for some reason it sends an request with method "OPTIONS" without a body. So i have to create a queryParams string myself. If anyone can explain this or show a better way to do this i would appriciate it. Otherwise this currently works:

     onSubmit = function (user) {
        console.log(user);
    
        var body = "firstname=" + user.firstname + "&lastname=" + user.lastname + "&name=" + user.name;
        this.http.post("http://www.testtttt.com", body).subscribe((data) => {});
    
      }
    

    Edit: another and probably even a better solution is to use JSON.stringify(user) instead of body. But subscribe() is still needed tho.

提交回复
热议问题