11

Having some trouble:

I do this simple test and the alert pops up the text "test return simple":

jQuery post:

$.post("http://www.localhost/webapi/api/corkboard/test/", jsonData)
            .done(function(data){
                alert(data);
        });

Asp.Net WebAPI:

[HttpPost]
public string test()
{        
    return "test return simple";
}

But when I change the WebAPI by adding a parameter:

public string test(string JSONData)
    {
        var jData = Json.Decode(JSONData);
        return "test return: " + jData.Filter;            
    }

I get the following error message:

"No HTTP resource was found that matches the request URI 'http://www.localhost/webapi/api/corkboard/test/'

Stuck and would appreciate any thoughts ... thanks !

nanonerd
  • 1,964
  • 6
  • 23
  • 49
  • What web server are you using as your backend? The specific technology should define how to handle post-methods and the data coming in as a POST request. It might not be as straighforward as just adding a parameter in your POST method handler. – amitsaurav Oct 28 '13 at 04:12
  • I am using .net WebAPI ... I'm trying to follow some existing code that I found that uses the same architecture: jquery post and .net webapi ... but I'm snagged on this part ... – nanonerd Oct 28 '13 at 04:29
  • I am not an expert on .net webapi but found a link which talks about how to configure your GET, POST and other HTTP handlers: http://www.codeproject.com/Articles/549152/Introduction-to-ASP-NET-Web-API – amitsaurav Oct 28 '13 at 04:49
  • Thanks Stony. I'll look at it later when I get a chance ... ;-) – nanonerd Oct 28 '13 at 06:02
  • I tried changing the first line in the jQuery request by adding the jsonData as a URL parameter and it worked. But I don't want to pass data in the URL. I guess I need to understand clearly how data is passed from client to server. Here's my change: $.post("http://localhost/webapi/api/corkboard/test?JSONData=" + jsonData) – nanonerd Oct 28 '13 at 06:29
  • No matter how you posted, if you don't want to create model for some reason, access all posted data from `HttpContext.Current.Request.Form.AllKeys` or specific key `HttpContext.Current.Request.Form["name"]` remember I'm posting data like this `$.post('url', { name: 'some', id: '123' })` – sairfan Jan 03 '19 at 20:27

2 Answers2

15

Change your WebApi method to:

public string test([FromBody]string JSONData)
    {
        var jData = Json.Decode(JSONData);
        return "test return: " + jData.Filter;            
    }

and your JQuery to:

$.post('http://www.localhost/webapi/api/corkboard/test/', { '': jsonData })
        .done(function(data){
            alert(data);
    });
Jon Susiak
  • 4,948
  • 1
  • 30
  • 38
  • 1
    Bravo, this works. Tks. But man, the syntax is really quirky and I was not familiar with it ... for further details, see the Encosia.com article in the post by J. Marley. - cheers ! – nanonerd Oct 29 '13 at 05:57
6

Try the following code..

$.post("http://www.localhost/webapi/api/corkboard/test/", { value: jsonData })
            .done(function(data){
                alert(data);
        });

Or, you can check the following link..

http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/