【问题标题】:Ajax is not getting response from callAjax 没有从调用中得到响应
【发布时间】:2017-12-24 14:09:22
【问题描述】:

我尝试了很多事情,但没有得到回应。 我正在尝试从 mydomin/token 检索令牌,并且它响应良好。我调试了它并且它工作正常,但 Ajax 没有得到响应。 我正在使用这个

<script>
    //working but not getting response
    function myFunction1() {
       $.ajax({
            type: "GET",
            url: "/Account/CheckLogin?Email=" + $("#username").val()
            + "&password=" + $("#password").val(),
            success: function (data) {
                alert(data);
            }
        }).done(function (data) {
            alert(data);
        }).fail(showError);
    }
</script>

这是我的服务器代码

 [HttpGet]
        public JsonResult CheckLogin(Account account)
        {
            Result obj = new Result();
            if (!account.Equals( null))
            {
                System.Diagnostics.Debug.Write(account.Email);
                System.Diagnostics.Debug.Write(account.Password);
                var result = db.Users.Any(x => (x.Username.Equals(account.Email) || x.Email.Equals(account.Email)) && x.Password.Equals(account.Password));
                if (result)
                {
                    //return RedirectToAction("Index", "Home");
                    obj.txt = true;
                    return Json(obj,JsonRequestBehavior.AllowGet);
                }
            }
            obj.txt = false;
            return Json(obj, JsonRequestBehavior.AllowGet);
        }

我在浏览器和邮递员中得到的响应是

{
txt: false
}

但是 ajax 没有对未显示警报的数据给出任何响应。即使我这样做是为了确保但没有显示任何警报

 success: function (data) {
                alert("Hi");
            } 

我关注了这个和堆栈上的其他一些链接,但对我没有用。 how to call controller action in ajax url

【问题讨论】:

  • 将 dataType: 'json' 添加到您的 ajax 调用中
  • 你是怎么调用myFunction1函数的?

标签: javascript jquery ajax asp.net-mvc response


【解决方案1】:

如果您看不到警报消息,则 ajax 调用不成功, 请尝试从浏览器(chrome)调试它 点击 f12 并检查是否有任何错误并选择网络选项卡然后滚动浏览向服务器发出的请求并找到由您的 ajax 调用发出的请求单击它然后您会发现问题所在 you should be able to find your ajax call here in red if it was failing

【讨论】:

【解决方案2】:

我的问题是我试图从具有服务器端点击事件设置的提交按钮的点击事件中触发 Ajax。我不得不让按钮只是一个简单的按钮(即)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-10
    • 2018-06-18
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    • 2015-10-31
    • 1970-01-01
    相关资源
    最近更新 更多