Siva Unique
Siva Unique

Reputation: 189

I am getting error when i am using rest api angular 2

I want to get data using rest api. i am passing my rest api but it showing error

    >      Cross-Origin Request Blocked: The Same Origin Policy disallows reading
    >     the remote resource at http://ip/JsonServices.php?action=getempdata.
    >     This can be fixed by moving the resource to the same domain or
    >     enabling CORS.

        **Below is my code**


  import { Injectable } from '@angular/core';
    import { Http ,Response} from '@angular/http';
    import 'rxjs/add/operator/map';

    @Injectable()
    export class WebserviceProvider {

      constructor(public http: Http) {

        console.log('Hello WebserviceProvider Provider');

      }

     getUser() {


      return this.http.get('http://ip/JsonServices.php?action=getempdata')

        .map((res:Response) => res.json());
      }

    }

Any body help me to get resolve above error?

Upvotes: 0

Views: 37

Answers (1)

H.H
H.H

Reputation: 364

I think it is cross domain issue.
If rest server and angular server are the same server. try below.

this.http.get('/JsonServices.php?action=getempdata')

if "ip" is external server. The server need to reply response header for CORS

I don't know your server code. but image of fix is below.

w.Header().Add("Access-Control-Allow-Origin", "*")
w.Header().Add("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
w.Header().Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")

Upvotes: 1

Related Questions