我現在正在使用MVC4,嘗試使用Jquery Ajax將數據插入本地服務器,在這裏我正面臨問題。 「ValueController」中的Post方法未被調用。請幫忙。發佈請求不能在MVC4中使用Ajax使用Jquery
這是我在View中的腳本。
<script src="~/Scripts/jquery-1.7.1.js"></script>
<script>
$('#createCustomer').click(function() {
debugger;
var id = $('#id').val();
var name = $('#name').val();
var city = $('#city').val();
var cust = {
Id: id,
Name: name,
City: city
}
$.ajax({
type: 'POST',
url: '/api/values',
data: JSON.stringify(cust),
datatype: 'JSON',
contentType: "application/json;charset=utf-8",
Success: function (result) {
success: true;
}
});
});
</script>
在 「
ValueController」
public HttpResponseMessage Post(Customer cust)
{
CustomerEntity.InsertValues(cust);
var response = Request.CreateResponse(HttpStatusCode.Created, cust);
return response;
}
這裏,客戶
我Post方法是模型
if action name is Post爲什麼我們要做api/value? –
@EhsanSajjad,'api/controllername',檢查http://www.asp.net/web-api/overview/web-api-clients/calling-a-web-api-from-a-net-client –
@ Murali你怎麼能假設使用WebApi概念的OP?有什麼理由嗎? – SivaRajini