Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular URLSearchParams not working for multiple params

I have an Asp.Net web api controller like this:

[HttpPost]
public IHttpActionResult RepHistorie([FromUri]string vnr, [FromUri]string lagerort)
{
    ...
}

So you can see that there are 2 values. I am using URLSearchParams for sending the parameters. Unfortunately this doesn't work for more than one param. I don't know why and this is really strange to me.

let params: URLSearchParams = new URLSearchParams();
params.set('vnr', this.vnr);
params.set('lagerort', this.lagerort);

var postUrl = 'http://123.456.7.89:12345/api/lager';

let requestOptions = new RequestOptions();
requestOptions.search = params;

this.http.post(postUrl, requestOptions)
  .subscribe(data => {
    // success

  }, error => {
    // error
});

In my other controller there is only one parameter and it works perfectly. Does someone know why?

I also tried:

params.append('vnr', this.vnr);
params.append('lagerort', this.lagerort);

Edit

It works like this (Isn't a clean way though):

var postUrl = 'http://123.456.7.89:12345/api/lager?vnr=' + this.vnr + '&lagerort=' + this.lagerort;
var postObject = {};
like image 558
Tom el Safadi Avatar asked Dec 18 '22 05:12

Tom el Safadi


1 Answers

I had the same issue before noticing that i had not imported it, you have to import URLSearchParams before using it.

import {URLSearchParams} from '@angular/http';

Enjoy coding!

like image 74
ErvTheDev Avatar answered Dec 29 '22 11:12

ErvTheDev