[英]How to use Ajax with asp.net MVC4
我在Asp.net中使用帶有Jquery Datatables的 X-Editable插件來編輯我的表。 我正在使用jQuery $ .ajax()方法對控制器中的方法進行調用,但是調用此函數時並未觸及該方法。 有人知道如何使用jQuery $ .ajax()方法嗎?
$('#example').dataTable();
"fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
$('td', nRow).wrapInner('<a class="xediable-example" href="#"></a>').editable({
type: 'text',
pk: 1,
name: 'test',
url: function (params) {
var urlj = "TestMethod"
return $.ajax({
type: 'POST',
url: urlj,
data: JSON.stringify(params),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
cache: false,
timeout: 10000,
});
}
});
return nRow;
}
});
控制方式:
public static string TestMethod(string name, string pk, string value)
{
return "" ;
}
錯誤信息 :
Erreur du serveur dans l'application '/'.La ressource est introuvable. Description : HTTP 404. La ressource recherchée (ou l'une de ses dépendances) a peut-être été supprimée ou renommée ou bien elle n'est plus disponible temporairement. Vérifiez l'URL ci-après et assurez-vous qu'elle est correcte. URL demandée: /H41_TitreConge/TestMethod Informations sur la version : Version Microsoft .NET Framework :4.0.30319; Version ASP.NET :4.0.30319.17929
var urlj = "TestMethod"
不是有效的URI。
將其更改為http://:// TestMethod,但是您可以設置環境。
如果您鍵入服務URL / TestMethod,只需嘗試在瀏覽器中訪問TestMethod,它實際上應該會給您一些合理的信息。
您的網址應為:
var urlj = '/YOUR_CONTROLLER_NAME/TestMethod'
您的測試方法必須是這樣的:
[HttpPost]
public JsonResult TestMethod(string name, string pk, string value)
{
return Json( ... )
}
嘗試這個:
var urlj = "@Html.Action("TestMethod")"
通過將URL更改為此也可以! 試試看,
url: '@Url.Action("ActionName","ControllerName")',
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.