[英]ASP.NET MVC error to use $.post(), this method put another controller name to the call
我有一個ASP.NET MVC應用程序。 當我使用$.pos()
時遇到問題。 此方法在第一個參數中接收控制器名稱和操作名稱,例如"Controller/Action"
。 問題是在某些地方無條件地post方法將控制器名稱添加到post方法。 我可以用螢火蟲觀察到這一點。 以下是我的源代碼示例:
$.post("Customer/Create", {}, function (data) {
$("#Detalle").html("");
$("#Detalle").append(data);
});
無條件呼叫包含"Customer/Customer/Create"
。 我可以在firebug中觀察它,這會導致錯誤,因為"Customer/Customer/Create"
不存在。 問題是什么? 我已經看到,如果我使用$.ajax
或().load
我沒有這個問題,但我更喜歡使用$.post
而不是$.ajax
因為這將導致一個很大的變化。 有人能解釋一下這個問題是什么嗎? 謝謝各位朋友。
而不是這個:
$.post("Customer/Create", {}, function (data) { $("#Detalle").html(""); $("#Detalle").append(data); });
試試這個,添加你url
的斜線開頭:
$.post("/Customer/Create", {}, function (data) { $("#Detalle").html(""); $("#Detalle").append(data); });
要避免與斜杠相關的錯誤或正確的路徑最佳方式,請使用如下所示的Url:
$.post('@Url.Action("Create","Customer")', {}, function (data) {
$("#Detalle").html("");
$("#Detalle").append(data);
});
或者如果使用的是經驗js文件而不是管理網址的最佳方式。在布局或視圖中創建全局變量,如下所示:
<Script>
var customerUrl = {
Create: '@Url.Action("Create","Customer")'
//Append mor url accoring to your requirement.
}
</script>
然后在你的外部js文件中使用以下:
$.post(customerUrl.Create , {}, function (data) {
$("#Detalle").html("");
$("#Detalle").append(data);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.