0

I am using Angular 2 in order to send HTTP requests to the server. The server is running with ASP.Net.

My API:

public class LagerController : ApiController
{
    public IHttpActionResult RepHistorie(string vnr, string lagerort)
    {
        ...
    }
}

So I would call the API with

http://123.456.7.89/api/lager?vnr=W17291092373&lagerort=0382691395

This works fine when using a tool called postman with which you can test API's.

But when making a post request with Angular 2, it doesn't work. It says that the HTTP-resource is not found.

Angular 2:

submit() {
  var body = 'vnr=W17291092373&lagerort=0382741400';

  var link = 'http://123.456.7.89/api/lager';
  this.http.post(link, body)
    .subscribe(data => {
      console.log(data);
    }, error => {
      console.log("Oooops!");
    });
}

It seems like the parameters aren't added correctly.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Tom el Safadi
  • 6,164
  • 5
  • 49
  • 102
  • Please use the angular tag for angular 2+ questions, not the angularjs tag. – Mike Feltman Jul 18 '17 at 20:13
  • It is still AngularJS, wether Angular 2 or Angular 1 so it is the correct tag. I don't know how your unnecessary comment should help me solving the problem though. @MikeFeltman – Tom el Safadi Jul 18 '17 at 20:16
  • It is not. Google is making this distinction and Stackoverflow is following suit. Please read the descriptions for the tags. This is the standard that StackOverflow has set for the use of these tags. Perhaps if you tag your question properly you'll get helpful answers & comments. :) – Mike Feltman Jul 18 '17 at 20:21
  • I changed it. Did you give me the downvote for it? @MikeFeltman – Tom el Safadi Jul 18 '17 at 20:26
  • Yes, and now I have removed it. This has been the policy for months. I'm trying to increase adherence. :) – Mike Feltman Jul 18 '17 at 20:31
  • Thanks. I read through the description of the tag and you are right. – Tom el Safadi Jul 18 '17 at 20:32

1 Answers1

1

This needs clarification, since the API above seems to be a GET Request.

In case it is a POST Request , then you should use the whole URL while running the API

In case you want to submit an object , you should use [FromBody] as a parameter Example

[HttpPost]
public IHttpActionResult( [FromBody]YourObject item ) {

}

==== Client-side

var postUrl = 'http://123.456.7.89/api/lager?vnr=W17291092373&lagerort=0382691395';
var postObject = {};
http.post(postUrl,postObject)

For GET request where you would like to use QueryString

[HttpGet]    
public IHttpActionResult RepHistorie([FromQuery]string vnr,[FromQuery]string lagerort){
                ...
}

====

// Query string can be built using RequestOptionsArgs as well
var builtUrl = 'http://123.456.7.89/api/lager?vnr=W17291092373&lagerort=0382691395';
http.get(builtUrl)

Alternative way is to

var getUrl = 'http://webapi/api/lager';
var requestOptions = {};
http.get(getUrl,requestOptions);

Reference:

Vi Elite
  • 136
  • 2
  • 6
  • Hi thanks for the long answer. I tried everything you said but it doesn't work... – Tom el Safadi Jul 18 '17 at 21:22
  • So is it a POST or a GET request ? – Vi Elite Jul 18 '17 at 21:24
  • It is a POST request – Tom el Safadi Jul 18 '17 at 21:24
  • You might want to edit your code above since it may look ambiguous for other reader whether it is a POST or GET request – Vi Elite Jul 18 '17 at 21:29
  • Yes I will edit it later and accept your answer. I still have to figure out what the problem caused and if there is a more elegant sultion than putting everything into that one 'builtUrl' string. Do you know that? I used the second last approach of you with the builUrl string with a post request though. – Tom el Safadi Jul 18 '17 at 21:32
  • How about [this](https://stackoverflow.com/questions/34475523/how-to-pass-url-arguments-query-string-to-a-http-request-on-angular-2) – Vi Elite Jul 18 '17 at 21:34