2015-08-26 116 views
0

我有WebApi應用程序和一個簡單的Web客戶端。我使用web客戶端的angularJS向webApi發送請求。當然,cors已經啓用了。XMLHttpRequest無法加載URL。無效的HTTP狀態代碼400(在Chrome上使用angularJs的WebApi)

我在Chrome上發佈Post有問題,但是我使用param來修正它發送的對象,我認爲它會和Put一樣,但是我得到'XMLHttpRequest無法加載URL。 Chrome瀏覽器無效的HTTP狀態代碼400',而它在IE上運行正常。

C#代碼:

public void UpdateLampe(int Id, Lampe lampe) 
    { 

     var context = new eDomDataContext(); 
     var found = context.Lampes.SingleOrDefault(p => p.Id == Id); 
     if (found != null) 
     { 
      found.Etat = lampe.Etat; 
      found.Date = DateTime.Now; 

      context.Lampes.Attach(found); 
      context.Entry(found).State = EntityState.Modified; 

      context.SaveChanges(); 
     } 
    } 

//Post request (works ok) 
 

 
var lampe = $.param({'TypeObject': typeObject, 'SalleId': salleId}); 
 
     $http({ 
 
      method: "POST", 
 
      url: "http://localhost:1770/api/Lampe", 
 
      data: lampe, 
 
      headers: {'Content-Type': 'application/x-www-form-urlencoded',} 
 
     }).success(function (data) { 
 
      alert("it works"); 
 
      }).error(function() { 
 
      console.log(Error); 
 
      alert('Error reading JSON file.'); 
 
     }) 
 
      .then(function (response) { 
 
        return response; 
 
      }); 
 

 

 
//Put request <= still have problem 
 

 
var etat = $.param({'Etat' : false}); 
 
$http({ 
 
      method: "PUT", 
 
      url: "http://localhost:1770/api/Lampe/1" , 
 
      data: etat, 
 
      headers: {'Content-Type': 'application/x-www-form-urlencoded',} 
 
     }).success(function (data) { 
 
      alert("it works"); 
 
      }).error(function() { 
 
      console.log(Error); 
 
      alert('Error reading JSON file. - '); 
 
     }) 
 
      .then(function (response) { 
 
        return response; 
 
      });

有什麼我做了什麼?

謝謝你的幫助。

回答

相關問題