Web method return OK but fire fail function

2019-09-14 18:11发布

问题:

here is my web method

[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);
        }   

and here is the ajax call

 $.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);                     
                    }

                });

it gives me result.status =200 and result.statusText = OK but it fire Error Event

回答1:

Please check that you are returning valid json or not, because you are setting

dataType: "json"

it evaluates the response as JSON and returns a JavaScript object. (...) The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown.

You may want to see this