[英]ASP.NET MVC error to use $.post(), this method put another controller name to the call
I have an ASP.NET MVC application. 我有一个ASP.NET MVC应用程序。 I have a problem when I use
$.pos()
. 当我使用
$.pos()
时遇到问题。 This method receives in the first parameter the controller name and action name, for example "Controller/Action"
. 此方法在第一个参数中接收控制器名称和操作名称,例如
"Controller/Action"
。 The problem is that in some place for no reason the post method adds the controller name to the post method. 问题是在某些地方无条件地post方法将控制器名称添加到post方法。 I can observe this using firebug .
我可以用萤火虫观察到这一点。 Here's an example from my source code:
以下是我的源代码示例:
$.post("Customer/Create", {}, function (data) {
$("#Detalle").html("");
$("#Detalle").append(data);
});
For no reason the call contains "Customer/Customer/Create"
. 无条件呼叫包含
"Customer/Customer/Create"
。 I can observe it in firebug and this causes an error because "Customer/Customer/Create"
doesn't exist. 我可以在firebug中观察它,这会导致错误,因为
"Customer/Customer/Create"
不存在。 What is the problem? 问题是什么? I have seen that if I use
$.ajax
or ().load
I don't have this problem, but I prefer to use $.post
instead of $.ajax
because will cause a great change. 我已经看到,如果我使用
$.ajax
或().load
我没有这个问题,但我更喜欢使用$.post
而不是$.ajax
因为这将导致一个很大的变化。 Can someone explain to me what is cuasing the problem? 有人能解释一下这个问题是什么吗? Thank you friends.
谢谢各位朋友。
Instead of this: 而不是这个:
$.post("Customer/Create", {}, function (data) { $("#Detalle").html(""); $("#Detalle").append(data); });
Try this, add a slash beginning of your url
: 试试这个,添加你
url
的斜线开头:
$.post("/Customer/Create", {}, function (data) { $("#Detalle").html(""); $("#Detalle").append(data); });
To avoid the slash related error or correct path best way use Url like below: 要避免与斜杠相关的错误或正确的路径最佳方式,请使用如下所示的Url:
$.post('@Url.Action("Create","Customer")', {}, function (data) {
$("#Detalle").html("");
$("#Detalle").append(data);
});
or if are using expernal js file than best way to manage url .Create global variables in Layout or View like below: 或者如果使用的是经验js文件而不是管理网址的最佳方式。在布局或视图中创建全局变量,如下所示:
<Script>
var customerUrl = {
Create: '@Url.Action("Create","Customer")'
//Append mor url accoring to your requirement.
}
</script>
and then in you external js file use following : 然后在你的外部js文件中使用以下:
$.post(customerUrl.Create , {}, function (data) {
$("#Detalle").html("");
$("#Detalle").append(data);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.