Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using jQuery post to ASP.Net webapi

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 !

like image 764
nanonerd Avatar asked Oct 28 '13 04:10

nanonerd


1 Answers

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);
    });
like image 15
Jon Susiak Avatar answered Oct 22 '22 20:10

Jon Susiak