【问题标题】:Web method return OK but fire fail functionWeb 方法返回 OK 但触发失败函数
【发布时间】:2023-03-06 18:40:01
【问题描述】:

这是我的网络方法

[HttpGet]
        public ActionResult EditEmp(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Employee Emp = db.Employees.Find(id);
            if (Emp == null)
            {
                return HttpNotFound();
            }
            ViewBag.dept_id = new SelectList(db.Departments, "dept_id", "dept_name", Emp.dept_id);
          return PartialView("_EditEmp", Emp);
        }   

这里是ajax调用

 $.ajax({
                    type: "GET",
                    url: '/Employee/EditEmp',
                    data: { id: idp },
                    dataType: "json",
                    success: function (result) {        
                        alert(result);
                        $('#editid').html(result);

                    },
                    error: function (result) {
                        alert("FAILED : " + result.status + ' ' + result.statusText);                     
                    }

                });

它给了我 result.status =200 和 result.statusText = OK 但它会触发错误事件

【问题讨论】:

    标签: javascript ajax web-services model-view-controller


    【解决方案1】:

    请检查您是否返回了有效的 json,因为您正在设置

    数据类型:“json”

    它将响应评估为 JSON 并返回一个 JavaScript 对象。 (...) 对 JSON 数据进行严格解析;任何格式错误的 JSON 都会被拒绝并引发解析错误。

    你可能想看看this

    【讨论】:

    • 非常感谢,你让我开心
    • 乐于助人。
    猜你喜欢
    • 2014-01-15
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    • 2012-01-22
    • 2013-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多