简体   繁体   中英

ERROR:options (url) 405 (method not allowed) and failed to load (url):response for preflight has invalid HTTP status code 405

I have tried to get the data from api using httpClient in angular 5 but i got errors. below *1)ERROR:options http://localhost:8080/api/getdata 405 (method not allowed)

2)ERROR:failed to load http://localhost:8080/api/getdata 405 (method not allowed):response for preflight has invalid HTTP status code 405*

Below is my service code

 import {Injectable} from '@angular/core'; import {HttpClient, HttpErrorResponse,HttpHeaders } from '@angular/common/http'; import { Http, Response } from '@angular/http'; import 'rxjs/add/operator/filter'; import 'rxjs/add/operator/map'; import 'rxjs/add/operator/catch'; @Injectable() export class DataService { url = "http://localhost:8080/api/getdata"; constructor (private httpClient: HttpClient) {} getList() { let headers = new HttpHeaders(); headers = headers.set('Content-Type', 'application/json').set('Access-Control-Allow-Origin','*').set('accept', 'application/json').set('Authorization', 'Basic bWdpcxxdferhWx1Xzkz'); return this.httpClient.get(this.url, {headers}); } } 

Where i did a mistake?

OPTIONS request should be handled from the server side. When you request the remote resource, the browser makes OPTIONS request before the actual request take place.

OPTIONS request will retrieve the allowed headers and if you haven't added your headers in the "Acess-Control-Allow-Headers" then it'll throw an error that 'some-header' is not allowed.

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