2015-10-05 119 views
0

我已經使用這篇文章Jquery Ajax Posting json to webservice通過「POST」web服務發送數據到服務器,但它已被引用爲「獲取」 「網絡「瀏覽器的部分,這是我得到:我使用「獲取」web服務而不是「POST」web服務

enter image description here

這是我的web服務(ASP.net)代碼:

// Insert Student 
     [Route("api/Students/ajout")] 
     [System.Web.Http.ActionName("Create")] 
     public async Task<HttpResponseMessage> Post(Student value) 
     { 

      try 
      { 

       if (ModelState.IsValid) 
       { 
        _StudentService.Insert(value); 
        var response = Request.CreateResponse<Student>(HttpStatusCode.Created, value); 
        await _unitOfWorkAsync.SaveChangesAsync(); 
        return response; 
       } 
       else 
       { 
        return Request.CreateResponse(HttpStatusCode.InternalServerError, "Model state is invalid"); 
       } 
      } 
      catch (Exception ex) 
      { 
       return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message); 
      } 
     } 

,這是我的代碼(AngularJS)

$.ajax({ 
     type: 'POST',  
     url : 'http://localhost:50001/api/Students/ajout', 
     dataType: 'jsonp', 
     contentType: "application/json; charset=utf-8", 
     data: { FirstName: ''+$scope.FirstName+'', LastName: ''+ $scope.LastName+'' , Email: ''+ $scope.Email+'' , DropOut:'' +$scope.dropout.value1+'' , Live: ''+$scope.live.value2+'' , ClassId:2, ImageId:1}, // On fait passer nos variables, exactement comme en GET, au script more_com.php 
     success: function(data){        
      console.log("success"); 
     }, 
     failure: function(errMsg) { 
     console.log(errMsg); 
    } 
    }); 

有你請任何想法,我怎麼能解決這個問題,非常感謝幫助

更新: 我已經改變了這樣的代碼是:

$.ajax({ 
     type: 'POST',  
     url : 'http://localhost:50001/api/Students/ajout', 
     dataType: 'json', 
     contentType: "application/json; charset=utf-8", 
     data: { FirstName: ''+$scope.FirstName+'', LastName: ''+ $scope.LastName+'' , Email: ''+ $scope.Email+'' , DropOut:'' +$scope.dropout.value1+'' , Live: ''+$scope.live.value2+'' , ClassId:2, ImageId:1}, // On fait passer nos variables, exactement comme en GET, au script more_com.php 
     success: function(data){ 

     $rootScope.usersData = angular.toJson(data); 
     console.dir($rootScope.usersData);         
      console.log("success"); 
     }, 
     failure: function(errMsg) { 
     console.log(errMsg); 
    } }); 

,但我得到這個錯誤: XMLHttpRequest無法加載http://localhost:50001/api/Students/ajout。預檢的響應具有無效的HTTP狀態代碼405

+1

我相信你無法通過JSONP – Nesh

+2

POST這是jQuery代碼不angularjs – masimplo

+0

感謝您的雷馬克mxa055我的意思angularJS,我把這個請求控制器:d – hanali

回答

1

在您的api中嘗試刪除[Route(「api/Students/ajout」)]而不是[System.Web.Http.ActionName(「Create」)] put [ HttpPost]。 在你的控制器中,我建議你使用angularjs查詢而不是ajax請求。

這個請求應該運行,如果您編輯API控制器:

[HttpPost] 
    public void CreateStudent([FromBody]Student value) 
    { 
     ... 
    } 

我:

var request = $http({ 
    url:"api/students/", 
    dataType:"json", 
    method:"POST", 
    data:JSON.stringify(student),//you can send directly the object student if is well-trained or use params:{'name':value, ...} 
    contentType:"application/json; charset=utf-8" 
}); 

而且在你的API控制器,如果您使用JSON.stringify改變你的方法來恢復你的對象不知道如果你這樣做,但在你的html中,而不是在你的輸入綁定屬性firstname,綁定student.firstName。像這樣,您可以直接使用屬性$ scope.student.firstName來恢復$ scope.student,而不是逐個恢復每個屬性。因此,直接發送像我放在例子中的對象。

所以我希望這會有幫助。 告訴我你的進度。祝你一天愉快。

+1

並在您的api控制器上方的班級名稱中添加'[Route(「api/[controller]」)]''這樣在您的請求中不再需要添加完整的url並只使用「api/students」。 – JonathanTheBrosh