0
return this.http.post(this._InstUrl, body, options)
                               .map((res: Response) => {res.json() })
                               .subscribe(
                                        (data) => {
                                            console.log(this.Result);
                                        },
                                        (error) => {
                                             console.log(error);
                                        });

When i execute above code, it didn't get work for me and showing below mentioned error.

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:55139' is therefore not allowed access. The response had HTTP status code 405.

Can anyone help me on this issue?

Vipul Upadhyay
  • 124
  • 1
  • 11
  • Possible duplicate of [Request doesn't pass access control check: No 'Access-Control-Allow-Origin'](http://stackoverflow.com/questions/36714906/request-doesnt-pass-access-control-check-no-access-control-allow-origin) – Akkusativobjekt Mar 31 '17 at 08:25
  • I think you should rename your question to something more instagram-specific. – n00dl3 Mar 31 '17 at 08:30

1 Answers1

2

Here example of post request for cross domain in angular 2

 this._InstUrl = 'https://api.instagram.com/v1/media/' + listOfMedia[0].id + '/likes?access_token=' + this.hdnaccess_token;
            let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' });
            var requestoptions = new RequestOptions({
                method: RequestMethod.Post,
                url: this._InstUrl,
                headers: headers,
            })

            return this.http.request(new Request(requestoptions))
                .map((res) => {
                    debugger;
                    if (res) {
                        //return { status: res.status, json: res.json() }
                    }
                })
                .subscribe(
                (data) => {
                    //if (this.Result.length == 0) { this.NoData = true; }
                    debugger; console.log(this.Result);
                },
                (error) => {
                    debugger; console.log(error);
                });
Vipul Upadhyay
  • 124
  • 1
  • 11
  • While this might has solved your problem, it is related to instagram API, not to a general CORS issue. It seems the intagram API does not allow CORS for any request not being of `Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'` – n00dl3 Mar 31 '17 at 08:28
  • The problem is not on the client side, because the server have to set the necessary header(s) for a cors request. And your code does not look like a jsonp request, which would be a possible solution – Akkusativobjekt Mar 31 '17 at 08:28