Click here to Skip to main content
15,898,731 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how pass parameters to web api through ajax call

this is my code,but it is passing null to name.why?what's wrong here?
JavaScript
function GetJson() {
    // Send an AJAX request
    $.ajax({
        type: "POST",
        url: "/api/Product",
        data:{name:'Hello'},
        dataType: 'json',
        success: function (response) {
            alert(response);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }
    });
}
Posted
Updated 11-Nov-13 1:00am
v2
Comments
Prasad Khandekar 11-Nov-13 7:15am    
This may be a binding error. Please have a look at this article (http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx).
Kornfeld Eliyahu Peter 11-Nov-13 7:48am    
If I'am remember well data should be a json string(!) and not json object...
So your line should look like
data: "{name:'Hello'}",

1 solution

data: JSON.stringify({name:'Hello'}); try this or look here for more
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900