简体   繁体   中英

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 !

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);
    });

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM