【问题标题】:routes not work using jquery ajax calling in mvc在 mvc 中使用 jquery ajax 调用路由不起作用
【发布时间】:2014-11-10 11:39:10
【问题描述】:

当我在 mvc 中使用自定义 routes.maps 时遇到问题,当我点击登录按钮时它显示错误 如下图所示

“/”应用程序中的服务器错误 无法找到该资源。 说明:HTTP 404。您正在查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请查看以下 URL 并确保其拼写正确。 请求的 URL:/ValidLogin**

但是当我使用localhost:49257/Admin/Login 时,它可以正常工作。 localhost:49257/Admin-login,显示错误如上图......

在下面定义自定义路由

routes.MapRoute(
    name: "admin login",
    url: "admin-login",
    defaults: new { controller = "Admin", action = "Login", id = UrlParameter.Optional } );

控制器

public ActionResult Login()
{
    return View();
}

public string ValidLogin(string username , string password)
{
    DAOAdmin dao = new DAOAdmin();
    var login = dao.IsValidAdmin(username, password);
    if (login != null)
    {
        return "done";
    }
    else
    {
        return "failed";
    }
}

下面的javascript代码

$(function () {
    $("input[name='btn']").click(function () {
        var dt = $(".fm").serialize();
        $.ajax({
            url: 'ValidLogin',
            type: 'GET',
            data: dt,
            success: function (result) {
                if (result.indexOf("done") > -1) {
                    location.replace("admin-section");
                } 
                else if (result.indexOf("failed") > -1) {
                }
            }
        });
    });
});

查看页面

@using (Html.BeginForm("Vlogin", "Admin", FormMethod.Post, new { @class = "fm" }))
{
    <label for="UserName">Username</label><br />
    @Html.TextBoxFor(a => a.username, new { @id = "txtuserid" })<br />

    <label for="Password">Password</label><br />
    @Html.PasswordFor(a => a.password, new { @id = "txtpassword" })<br />

    <input type="button" name="btn" value="Login" />
    <span id="result"></span>
}

【问题讨论】:

  • url: 'ValidLogin', 更改为 url: '@Url.Action("ValidLogin", "Admin")', 以构造正确的 url。我怀疑location.replace("admin-section"); 也可能导致问题。

标签: jquery .net ajax asp.net-mvc asp.net-mvc-routing


【解决方案1】:
URL should be like controller/action/id (id is optional)

So, try this:
routes.MapRoute(
    name: "admin login",
    url: "{controller}/{action}/{id}"
    defaults: new { controller = "Admin", action = "Login", id = UrlParameter.Optional } );

JS file:

$(function () {
    $("input[name='btn']").click(function () {
        var dt = $(".fm").serialize();
        $.ajax({
            url: 'Admin/ValidLogin',
            type: 'GET',
            data: dt,
            success: function (result) {
                if (result.indexOf("done") > -1) {
                    location.replace("admin-section");
                } 
                else if (result.indexOf("failed") > -1) {
                }
            }
        });
    });
});enter code here

In View Page, change Vlogin to Login.

@using (Html.BeginForm("Login", "Admin", FormMethod.Post, new { @class = "fm" }))
{
    <label for="UserName">Username</label><br />
    @Html.TextBoxFor(a => a.username, new { @id = "txtuserid" })<br />

    <label for="Password">Password</label><br />
    @Html.PasswordFor(a => a.password, new { @id = "txtpassword" })<br />

    <input type="button" name="btn" value="Login" />
    <span id="result"></span>
}

【讨论】:

    【解决方案2】:

    不应该吗

    url: "{controller}-{action}"
    

    而不是你的?

    【讨论】:

      猜你喜欢
      • 2019-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-24
      • 2012-02-13
      • 2017-02-20
      • 1970-01-01
      相关资源
      最近更新 更多