1

CODE SAMPLE 1

passJSONObj(){
  let name = 'user names',
  let email = '[email protected]'

  return this._http.get('url?name=usernames&[email protected]');

}

CODE SAMPLE 2

passJSONObj(){
let param = {
    name: 'usernames',
    email: '[email protected]'
}
return this._http.get('url', param);

}

Code sample 1 works fine but code sample 2 does not. I want to know the reason. I've made research but I haven't understand why such a simple code won't work

3
  • get simply does not have a parameter for json data, if you need to send additional data with your request, you can for example use post Commented Sep 30, 2020 at 13:51
  • @Mike thanks for your response. I know that get method does not have a parameter for JSON data. I want to know the reason behind it. Commented Sep 30, 2020 at 13:54
  • I think it's up to your Backend headers for Access-Control-Allow to accept and parse the data properly Commented Sep 30, 2020 at 14:25

1 Answer 1

2

The second parameter is not only for "query params". So you can try something like this

return this._http.get('url', { params : param});

See the type documentation :

 /**
     * Construct a GET request which interprets the body as text and returns the full response.
     *
     * @return an `Observable` of the `HttpResponse` for the request, with a body type of `string`.
     */
    get(url: string, options: {
        headers?: HttpHeaders | {
            [header: string]: string | string[];
        };
        observe: 'response';
        params?: HttpParams | {
            [param: string]: string | string[];
        };
        reportProgress?: boolean;
        responseType: 'text';
        withCredentials?: boolean;
    }): Observable<HttpResponse<string>>;
Sign up to request clarification or add additional context in comments.

6 Comments

I've tried that before, the parameters are not seen at the backend.
if you open your navigator "dev console" can you see those parameters in the network tab ?
Yes I can see the parameters and their data in the network tab
does it look like : '...........url?name=usernames&[email protected]' ?
ok so you problem seem not to be on the angular side. What kind of backend do you have and how do you try to get those parameters ?
|

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.