user1826633
user1826633

Reputation:

Passing Multiple Parameters to ASP.NET Web API With jQuery

POST form variables to simple parameters of a Web API method in ASP.NET MVC 4 Web API

$.ajax({
    url: 'api/products',
    type: 'POST',
    data: { Id: 2012, Name: 'test', Category: 'My Category', Price: 99.5 },
    dataType: 'json',
    success: function (data) {
        alert(data);
    }
});

but its not working how to do this?

Upvotes: 4

Views: 5373

Answers (1)

Durgesh Chaudhary
Durgesh Chaudhary

Reputation: 1077

Use below code it will work. Only change I have made is in data parameter where I am doing JSON.stringify(), faced with same issue quite a few months back. Basically it expects a string which can be parsed to JSON.

$.ajax({
    url: 'api/products',
    type: 'POST',
    data: JSON.stringify({ Id: 2012, Name: 'test', Category: 'My Category', Price: 99.5 }),
    dataType: 'json',
    success: function (data) {
        alert(data);
    }
});

Upvotes: 1

Related Questions