angular - 405 method not allowed error on foursquare post request in angular4 -


i trying upload foursquare user's profile photo angular web application. using "users/update" end point - https://developer.foursquare.com/docs/users/update

here template,

<input type="file" (change)="filechange($event)" placeholder="upload file"> 

here component code,

filechange(event) {      let fd:formdata=new formdata();     fd.append("photo",event.target.files[0]);      let headers=new headers();     headers.append("content-type","multipart/form-data");     headers.append("accept","application/json");     headers.append("access-control-allow-origin","true");       let options=new requestoptions({headers:headers});       this.http.post("https://api.foursquare.com/v2/users/self/update?oauth_token=myauthtoken&v=20160108",fd,options)     .map(response=>response.json())     .subscribe(                                                    data=>console.log(data),              error=>console.log(error)                                          ); } 

i getting 405 (method not allowed) , response preflight has invalid http status code 405 errors in console.

you want start removing following frontend javascript code:

headers.append("access-control-allow-origin","true") 

access-control-allow-origin response header servers send; effect adding request header ever have trigger cors preflight options request that’ll fail.

what see happening because code adds access-control-allow-origin request header, browser sending cors preflight options request; , browser consider preflight successful, https://api.foursquare.com/v2/users/self/update endpoint must respond options request 200 ok or 204 status code.

but 405 “(method not allowed)” response you’re getting instead indicates foursquare api endpoint isn’t configured handle options requests. preflight fails , browser never moves on doing post request code trying send.

however, responses non-options requests foursquare api endpoint include access-control-allow-origin response header. code in question should work expected long you’re not adding access-control-allow-origin client side, , long there’s no other characteristic of request trigger browsers preflight.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -