調用上的一個按鈕該功能點擊 角函數從angularjs到WCF傳遞JSON數據被拋出400壞請求錯誤
$scope.CreateNewTopic = function() {
var userdata = {
TopicName:$scope.topicname,
TopicDescription: $scope.topicdescription,
OriginalPosterID: $scope.userid,
CategoryID: $scope.selectedcategory
};
DataService.InsertTopicObject(userdata)
服務方法如下
InsertTopicObject: function (topic) {
$http({
method: 'POST',
url: connectionurl + 'InsertNewTopic',
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' },
data: topic
})
.success(function (data, status, headers, config) {
// successcallback(data);
console.log(data);
})
.error(function (data, status, headers, config) {
$log.warn(data, status, headers, config);
})
}
WCF
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/InsertNewTopic", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Wrapped)]
int InsertNewTopic(InsertNewTopic InsertTopicObject);
public int InsertNewTopic(InsertNewTopic InsertTopicObject) {
return SqlHelper.ExecuteNonQuery(SqlConnectionString.GetConnection(), CommandType.StoredProcedure, Constants.SP_Name.ToString(), new SqlParameter("TopicName", InsertTopicObject.TopicName), new SqlParameter("TopicDescription", InsertTopicObject.TopicDescription), new SqlParameter("OriginalPosterID", InsertTopicObject.OriginalPosterID), new SqlParameter("CategoryID", InsertTopicObject.CategoryID));
}
下面的錯誤或者我得到。
API要求的內容類型的Json 。嘗試刪除「Content-Type」標題。 (或者甚至刪除所有'header:{'Content-Type':'application/x-www-form-urlencoded; charset = utf-8'},') –
謝謝阿列克謝。我試着一樣,它不工作得到相同的錯誤。請幫幫我。 – Aravind