1

Hopefully someone can help with this. I building an ionic 2 app which is based on the newer Angular 2, I am familiar with previous versions of Angular, but still trying to figure this whole typescript.

I have my API setup with basic get querystrings (e.g domain.com?state=ca&city=somename)

export class TestPage {
public state: string ='ca';
public city: string = null;
constructor(private http: Http){}

public submit() { 
   let url = "http://localhost/api"; 
   let payload = {"state": this.state, "city": this.city};
   this.$http.get(url, payload).subscribe(result => {
      //result
   }, err => {  
      //do something with the error 
   }
  )
 }
}

When I execute this it pulls my API url fine and I can get a response back, however none of the querystrings are being sent in the request. Its just sending http://localhost/api. If I console.log the payload its fine.

Ultimately I am trying to get it to do https://localhost/api?state=ca&city=example

Looking at examples I can't really find anything straight-forward on this.

Is it not possible to take a payload on http with this newer version of Angular? The code above is just an example. I have many querystrings, which is why I was hoping to send a payload to it.

Any help or suggestion would be appreciated.

9
  • 1
    ionic 2 is based on angular 2.. not angular 4.. Commented Mar 27, 2017 at 9:29
  • Angular 4 is meant to be angular 2.4 I think. Googling it yield result that explain that. Angular.js is v1 and Angular 2 is v2 there isn't any v3 or v4 but some people, for some reason, call 2.3 and 2.4 Angular 3 and 4. Commented Mar 27, 2017 at 9:33
  • @GillesC no. The version is 4.0.0. Not 2.4.0. Angular now uses semantic versioning, and the 4.0 is an evolution of the 2.x version. Commented Mar 27, 2017 at 9:34
  • @GillesC Angular 4.0.0 changelog and angular 2.4.8 changelog Commented Mar 27, 2017 at 9:37
  • 1
    @limit: read the API documentation: angular.io/docs/ts/latest/api/http/index/Http-class.html. It should be this.http.get(url, { params: payload }) Commented Mar 27, 2017 at 9:38

1 Answer 1

1

The Http.get method takes an object that implements RequestOptionsArgs as a second parameter.

The search field of that object can be used to set a string or a URLSearchParams object.

An example:

// Parameters obj-
 let params: URLSearchParams = new URLSearchParams();
 params.set('state', this.state);
 params.set('city', this.city);

 //Http request-
 return this.http.get('http://localhost/api', {
   search: params
 }).subscribe(
   (response) => this.onGetForecastResult(response.json()), 
   (error) => this.onGetForecastError(error.json()), 
   () => this.onGetForecastComplete()
 );

Documentation: here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the explanation. This worked. I wonder if it get be looped off a single payload, so I don't have to set every param.
You can apply extra logic for that. Cheers!

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.