Ajax无法在服务器上找到操作,但在本地工作

时间:2014-11-06 15:18:36

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

我有一个ajax电话:

$(document).ready(function () {
        $("form#form").submit(function (e) {
            var request = $.ajax({
                type: "POST",
                url: '/Admin/SecureLogin/',
                data: { tdn: $('#tdn').val(), pw: $('#pw').val(), gn: $('#gn').val() },
                success: function (data) {
                    if(data == "Login failed"){
                        $('#error').text("Tên đăng nhập hoặc mật khẩu không đúng");
                    }
                    if(data == "OK"){
                        window.location.replace("/Admin/Home");
                    }
                }
            });
            request.fail(function (jqXHR, textStatus) {
                alert("Request failed: " + textStatus);
            });
            e.preventDefault(); // this is better than "return false"
        });
    });

和控制器:

public class AdminController : Controller
{
[HttpPost]
    public string SecureLogin(string tdn, string pw, string gn)
    {
        Car4RentContext db =  new Car4RentContext();
        var query = (from l in db.Account where l.UN == tdn && l.PW == pw select l).SingleOrDefault();
        if (query != null)
        {
            if (gn == "")
            {
                FormsAuthentication.SetAuthCookie(query.UniqueId.ToString(), false);
            }
            else
            {
                FormsAuthentication.SetAuthCookie(tdn, true);
            }
            //return RedirectToAction("Home","Admin");
            return "OK";
        }
        else
        {
            return "Login failed";
            //return View();
        }
    }
    public ActionResult Home()
    {
        return View();
    }
}

当我在我的本地机器上测试时,这个ajax运行良好。但是当我在主机上部署我的网站时,我登录并始终返回:

POST http://example.com/Admin/SecureLogin/ 404(未找到)

我已经阅读了这个问题:can't find action only on live server, works fine in local server但它不能与我合作。

以下是我的观点:     

        <form id="form" class="form-vertical login-form" novalidate="novalidate" style="display: block;">
            <h3 class="form-title">Thông tin đăng nhập</h3>

            <div class="form-group">
                <div class="input-group">
                    <div class="input-group-addon"><span class="glyphicon glyphicon-user"></span></div>
                    <input id="tdn" class="form-control" type="text" name="tdn" placeholder="Tên đăng nhập">
                </div>
            </div>
            <div class="form-group">
                <div class="input-group">
                    <div class="input-group-addon"><span class="glyphicon glyphicon-link"></span></div>
                    <input id="pw" class="form-control" type="password" name="mk" placeholder="Mật khẩu">
                </div>
            </div>
            <div class="form-group">
                <div id="error" class="input-group" style="color:red">
                </div>
            </div>
            <div class="form-actions">
                <div class="pull-left">
                    <input id="gn" type="checkbox" name="ghinho"> <span>Ghi nhớ</span>
                </div>
                <button class="submit btn btn-primary pull-right">
                    Sign In <span class="glyphicon glyphicon-chevron-right"></span>
                </button>
            </div>
        </form>
    </div>

对此错误的任何想法?

1 个答案:

答案 0 :(得分:0)

最后,我找到了它:)

问题是:当我创建新动作或编辑某些内容并将文件上传到服务器时,我忘了构建它。在本地,我运行(Crtl + f5),项目是自动构建的,因此它可以在本地而不是在服务器上工作。

解决方案:每当我更改某些内容时,我都会上传已更改的文件以及内置bin dicrectory。一切都像魅力一样:)