1

I tried to send post request with FormData using Angular2. But my webserver reach nothing and send me back error code 500. I checked request with PostMan util (GoogleChrome addons). And i found that i reach 500 only when i send nothing.

let Form = new FormData();
    Form.append("username", this.username);
    Form.append("password", this.password);

let headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');

    this.http.post("/ajax/rest_login",
        Form,
        {headers: headers})
    .map((res: Response) => res.json())
    .subscribe(
            res => console.log(res.text()),
            err => console.log(err),
            () => console.log('done'));

What i'am doing wrong? Maybe you know better way to post FormData

1 Answer 1

1

You shoud use the URLSearchParams class to do that:

let form = new URLSearchParams();
form.set("username", this.username);
form.set("password", this.password);

let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');

this.http.post("/ajax/rest_login",
    form.toString(),
    {headers: headers})

At the moment, the FormData class isn't supported out the box by the HTTP support of Angular2.

Don't forget to import the Headers class:

import {Http,Headers,URLSearchParams} from 'angular/http';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.