0

I'm having a problem that I can't seem to figure out. I am making the following ajax call:

    var dataArray = { table: "Products" };

        $.ajax({
            url: uri,
            type: "POST",
            cache: false,
            datatype: "json",
            data: dataArray
        })
        .done(function( msg ) {
                 alert( "Data Saved");
             })
        .fail(function () {
            alert("error");
        });

The issue I'm having is that this doesn't seem to be sending what I have in dataArray to my Web API controller, and keeps coming back with the following response:

{"Message":"No HTTP resource was found that matches the request URI 'http://myURI'.","MessageDetail":"No action was found on the controller 'MYController' that matches the request."}

The thing that is confusing me is that I can do a GET like this, and it returns data fine. Also if I just flatout append the querystring to the uri like

http://myURI?table=Products

then the POST works fine. I could do it this way, but I am pretty curious as to what I am doing wrong in the way I have above. Thanks!

This is what is in my controller. I'm just basically trying to verify I can get to it right now.

    public void Post(string table)
    {
        string beingHit = "We did it!";
    }
chuckw87
  • 407
  • 1
  • 4
  • 12
  • `datatype: json,` - you need quotes around the JSON bit - but if you say that you can POST to your endpoint fine when you have a GET variable on your query string, you need to evaluate the way your API controller is handling request methods - and post some of that code here so we can help you with it – scrowler Feb 16 '14 at 20:41
  • You also need to jsonify your data. $.ajax won't do that automagically for you. Use something like https://github.com/douglascrockford/JSON-js – Matthew Cox Feb 16 '14 at 20:41
  • @scrowler see http://stackoverflow.com/questions/13735869/datatype-application-json-vs-json – Musa Feb 16 '14 at 20:45
  • @scrowler, I actually do have json in double quotes in my code, just made an error putting it here. Posting what I am trying to get to in my controller shortly – chuckw87 Feb 16 '14 at 20:47
  • @Musa - what's your point? – scrowler Feb 16 '14 at 20:49
  • @scrowler you don't know what `datatype: json,` means. – Musa Feb 16 '14 at 20:56
  • @chuckw87 Can you edit your question with the programming language tag. – Rahil Wazir Feb 16 '14 at 20:57
  • Updated my post, I have a separate method for the get that is being reached just fine when I use GET in ajax call. – chuckw87 Feb 16 '14 at 20:58

1 Answers1

0

The issue is not necessarily that you aren't sending the data to Web API. It is that Web API does not recognize it.

When deciding what Action to match a request against and how to bind parameters, the following rules are applied by default by Web API (from the documentation on asp.net):

By default, Web API uses the following rules to bind parameters:

  • If the parameter is a “simple” type, Web API tries to get the value from the URI. Simple types include the .NET primitive types (int, bool, double, and so forth), plus TimeSpan, DateTime, Guid, decimal, and string, plus any type with a type converter that can convert from a string. (More about type converters later.)
  • For complex types, Web API tries to read the value from the message body, using a media-type formatter.

In your case you are trying to bind a "simple" type parameter (string). You have seen it working through the querystring. However, if you try to submit it as part of the request body. it will not be recognized by Web API by default.

In order to get Web API to recognize the parameter in the body of the request, you will need to indicate this in the Action signature using the FromBody attribute:

public void Post([FromBody] string table)
{
    string beingHit = "We did it!";
}

This will tell Web API to look at the request body for the param value (note that you can only use this for one param in the Action, so for simple types like this, it might be better to just include them in the query string, while [FromBody] is ideal for binding against a complex type included in the body of the request as Json).

Yaakov Ellis
  • 38,048
  • 29
  • 119
  • 167
  • Thank you very much for the explanation, and the link to the documentation. Really explained things well, and helped me get past the error. I am just going to use a query string for this then. – chuckw87 Feb 16 '14 at 22:52