4

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 = {};

2 Answers 2

11

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!

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

3 Comments

I don't know how much time I wasted with that!
This did it for me also, it's weird that my editor didn't throw a fit like it usually does whenever I try using something without importing it.
@Jon Probably because there's a native one: developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/…
0

It works for me with the following

    let params: URLSearchParams = new URLSearchParams();
    params.set('vnr', this.vnr);
    params.set('lagerort', this.lagerort);
    let requestOptions = new RequestOptions();
    requestOptions.params = params; // instead of requestOptions.search = params

Comments

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.