2015-10-15 205 views
-1

數據假設要插入到數據庫中,但沒有。任何進程之前,JQuery都會提示失敗的操作我認爲url根本不會向控制器發送請求,任何人都可以告訴我這段代碼中的錯誤在哪裏。數據未插入到數據庫中

public class EmployeeController : ApiController { 
    static EmpRepository repository = new EmpRepository(); 
    public string AddEmployee(Employee em) { 
     var response = repository.AddEmployees(em); 
     return response; 
    } 
} 

的Jquery:

function AddEmp() { 

    var Emp = {}; 
    Emp.FirstName = $("#fname").val(); 
    Emp.LastName = $("#lname").val(); 
    Emp.Company = $("#company").val(); 

    $.ajax({ 
     url = 'api/AddEmployee'; 
     tpye: "post", 
     contentType: "application/json;charset=utf-8", 
     data: JSON.stringify(Emp), 
     dataType: "JSON", 
     success: function (response) { 
      alert(response); 
     }, 

     error: function(x, e){ 
      alert('Failed'); 
      alert(x.response); 
      //alert(x.status); 
     } 
    }); 
} 

$(document).ready(function() { 

    $("#save").click(function (e){ 
     AddEmp(); 
     e.preventDefault(); 
    }); 

}); 
+0

我想知道爲什麼有人在沒有任何理由的情況下回答這個問題 –

+0

爲什麼你不把你的網址添加到$ .ajax調用? – Omidam81

+0

:),你編輯了你的代碼? – Omidam81

回答

1

調試的一小時,測試一個錯誤後,在URL中發現,這是不是在正確的格式,正確的格式是url = 'api/Employee/AddEmployee';

+1

謝謝你的回答。但這不是答案。沒人能猜到你的網址是什麼:) – Omidam81

+0

你也應該檢查'contentType:「spplicstion/json; charset = utf-8」' – Romulo

+0

這個巨大的錯字,這是真的,但主要的錯誤是網址,爲什麼我發佈控制器類在這裏。 –

1
function AddEmp() { 

var Emp = {}; 

url = 'api/AddEmployee'; 
Emp.FirstName = $("#fname").val(); 
Emp.LastName = $("#lname").val(); 
Emp.Company = $("#company").val(); 

$.ajax({ 

    tpye: "post", 
    url:url, 
    contentType: "spplicstion/json;charset=utf-8", 
    data: JSON.stringify(Emp), 
    dataType: "JSON", 
    success: function (response) { 

     alert(response); 
    }, 

    error: function(x, e){ 
     alert('Failed'); 
     alert(x.response); 
     //alert(x.status); 
    } 
}); 
} 

$(document).ready(function() 
{ 

$("#save").click(function (e){ 
    AddEmp(); 
    e.preventDefault(); 

}); 
+0

它顯示此錯誤'NS_ERROR_NOT_AVAILABLE:alert('Failed');' –

+0

http://stackoverflow.com/questions/17049176/ns-error-not-available-component-is-not-available – Omidam81

+0

所以問題不是你的jQuery代碼。 – Omidam81