2014-11-08 77 views
1

我試圖從angularjs控制器提交表單爲$ http.post()來asp.net網站API方法。但它發送空值。這裏是我的代碼

//angular controller 
$scope.newpost = {}; 
$scope.save = function() { 
    console.log($scope.newpost); // it logs data accurately 
    $http.post('/api/NewPost', $scope.newpost). 
     success(function (data, status, headers, config) { 
      alert("ok"); 
     }); 
} 


//api 
public HttpResponseMessage post(NewPost newpost) //fields are null here. I tried [FromBody] $ [FromURI], but no luck 
{ 
    posts.Add(newpost); 
    String id = newpost.Id; //saving is ok. 
    return Request.CreateResponse(HttpStatusCode.OK); 
} 


//model 
public class NewPost 
{ 
    public String Title { get; set; } 
    public String Content { get; set; } 
    public String Tags { get; set; } 
} 

console.log($scope.newpost) displays- 
Object {Title: "t", Content: "tt", Tags: "ttt"} 

任何幫助嗎?

+1

顯示的'的console.log($ scope.newpost);'的屬性名稱必須匹配,也POST之前做'JSON.stringify' – Max 2014-11-08 10:38:53

回答

0

使用本:

console.log($scope.newpost.Title); // it should logs data accurately 
console.log($scope.newpost.Content);// it should logs data accurately 
console.log($scope.newpost.Tags );// it should logs data accurately 
$http.post('/api/NewPost', $scope.newpost). 
    success(function (data, status, headers, config) { 
     alert("ok"); 
    }); 
+0

它不工作。 – 2014-11-08 10:43:32

+0

請張貼$ scope.newpost數據。 – 2014-11-08 10:53:55

+0

請參閱更新。 – 2014-11-08 10:59:55

相關問題