R
R
Roman2017-01-15 11:13:35
go
Roman, 2017-01-15 11:13:35

How to CORS POST them to Angular2 with application/json mime type?

From Angular I make a POST like this:

export class AuthenticationService {
    public token: string;
    private headers = new Headers({'Content-Type': 'application/json'});
...
login(email: string, password: string): Observable<boolean> {
        return this.http
                    .post("http://localhost:3500/login", JSON.stringify({ email: email, password: password }), this.headers)
...

the front spins on localhost:3000, i.e. as I understand it, the OPTIONS request must first be sent, tk. it's cors and content-type 'application/json' but instead sends a normal POST with content-type 'text/plain'
General:
Request URL: localhost:3500/login
Request Method:POST
Status Code:400 Bad Request
Remote Address: [::1]:3500
Response Headers
Content-Length:83
Content-Type:application/json; charset=UTF-8
Date:Sun, 15 Jan 2017 08:03:35 GMT
Request Headers
Accept:*/*
Accept-Encoding:gzip, deflate, br
Accept-Language:ru-RU,ru;q=0.8,en- US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:45
content-type:text/plain
Host:localhost:3500
Origin: localhost:3000
Referer: localhost:3000/login
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/55.0.2883.87 Chrome/55.0.2883.87 Safari/537.36
Request Payload
{email: "[email protected]", password: "user1"}
email: "[email protected]"
password: "user1"
Several questions: why is OPTIONS not sent? How to make application/json come to the back-end?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
planc, 2017-01-15
@losaped

the frontend does everything right for you
options the browser itself sends
kill your server and look at nc -lp 3500
the backend should answer with the headers that all the rules
https://developer.mozilla.org/en-US/docs/Web/HTTP/...
your last parameter is wrong

post("http://localhost:3500/login", JSON.stringify({ email: email, password: password }), {headers: this.headers})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question