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 !

6
  • 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. Commented Oct 28, 2013 at 4: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 ... Commented Oct 28, 2013 at 4: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: codeproject.com/Articles/549152/Introduction-to-ASP-NET-Web-API Commented Oct 28, 2013 at 4:49
  • Thanks Stony. I'll look at it later when I get a chance ... ;-) Commented Oct 28, 2013 at 6: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("localhost/webapi/api/corkboard/test?JSONData=" + jsonData) Commented Oct 28, 2013 at 6:29

2 Answers 2

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);
    });
Sign up to request clarification or add additional context in comments.

1 Comment

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 !
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/

2 Comments

The link has valuable and well explained details. Thanks.
Chrome says that link also contains malware just so ya know.

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.