2

I am trying to get some data from the server through REST API service. Inside the service.ts I have below,

    getCategories(): Observable<Category> {

// Http Options
    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin' : '*',
        'Access-Control-Allow-Methods' : 'POST'
      })
    };
        return this.http.post<Category>(this.apiURL+'api', JSON.stringify({"type":"get_categories"}), httpOptions )
        .pipe(
          retry(0),
          //catchError(this.handleError)
        )
      }

Which again shows the error as below,

Object { headers: {…}, status: 0, statusText: "Unknown Error", url: "http://localhost/test/api", ok: false, name: "HttpErrorResponse", message: "Http failure response for http://localhost/test/api: 0 Unknown Error", error: error }

I have been referring lots of posts, but nothing helps.

1 Answer 1

1

You get this error

ERROR ReferenceError: "httpOptions is not defined"

because you are using the httpOptions that defined outside of the scrope of getCategories

You should change your code like this.

getCategories(): Observable<Category> {
   const httpOptions = {
        headers: new HttpHeaders({
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin' : '*',
        'Access-Control-Allow-Methods' : 'GET, POST, PUT, DELETE'
        })
    };

   return this.http.post<Category>(this.apiURL+'api', JSON.stringify({"type":"get_categories"}), httpOptions )
    .pipe(
      retry(0),
      //catchError(this.handleError)
    )
  }
Sign up to request clarification or add additional context in comments.

8 Comments

This addresses one point. Now this section is ok. But even if follow this or passing the headers directly inside the request I am getting error as Http failure response. Please refer the second section in the query.
It's really hard to tell because I dont know you handle the API side code
Something is strange. If I pass headers, request itself not reaching the API server. Request method shows as OPTIONS instead of POST if I pass the header
Do you run angular and server jn different port ?
Yes, angular and API (Basically laravel on wamp) running on different port
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.