简体   繁体   中英

Angular won't post data to API

I got a problem which I dont understand. I try to post data to my API in a form using the following code

formSubmit() {

  const req =this.http.post('http://[ip]/api/login', {
  id: '7',
      username: 'PostTest',
      password: 'studp123lan',
      matrikelnr: 'winf303666',
      email: 'winf303666@example.de',
      email_verified: '1'
    })
      .subscribe(
        res => {
          console.log(res);
        },
  err => {
      console.log("Error occured");
  }

When I inspect it in the Chrome Developter tools, this is what I get:

Failed to load http://[ip]/api/login: Response for preflight has 
invalid HTTP status code 404
register.component.ts:42 Error occured

And this is what I get in the network tab:

General:

Request URL:http://[ip]/api/login
Request Method:OPTIONS
Status Code:404 Not Found
Remote Address:[ip]:80
Referrer Policy:no-referrer-when-downgrade

Response Header:

Access-Control-Allow-Headers:Content-Type
Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin:*
Content-Length:0
Date:Thu, 21 Dec 2017 09:00:35 GMT
Server:Kestrel
X-Powered-By:ASP.NET

Request Header:

Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
Access-Control-Request-Headers:content-type
Access-Control-Request-Method:POST
Connection:keep-alive
Host:[ip]
Origin:http://localhost:4200
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 
(KHTML, like Gecko) Chrome/63.0.3239.108 Safari/537.36

Somehow, this doesn't work. But when I Post the same data via Postman Post Request to the same URL, it works like a charm. Can anyone explain and help? Thanks.

404 is a page not found error : this means your endpoint isn't available at this address.

Sure, you make a postman call and it works : but did you create your postman call by hand, or used the interceptor to make it ? (The interceptor is a Chomr plugin that allows you to register all calls made by Chrome, into postman).

There must be something you have forgotten. Could you post your postman call, and if you can, try with the interceptor ?

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