【发布时间】:2018-12-17 03:56:38
【问题描述】:
以下简单的插入记录方法可以在 Postman 中使用,但我无法在我的 AngularJS 控制器中使用。
Postman Headers (Post) Key ContactID Value 0 Key FName Value John Key LName Value Smith Key Cache-Control Value no-cache Key Content-Type Value application/x-www-form-urlencoded
$scope.insert = function () {
var obj = { "ContactID": 0, "FName": "John", "LName": "Smith" };
var url = "http://********************.net/api/create";
$http({
method: 'POST',
url: url,
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
dataType: 'json',
contentType: 'application/json',
data: obj
});
}
【问题讨论】:
-
你绝对需要它是
x-www-form-urlencoded吗?您需要先serialise it。否则有$http.post(url, obj)就足够了 -
我的 C# 方法 [System.Web.Http.AcceptVerbs("GET", "POST")] [System.Web.Http.HttpPost] [System.Web.Http.Route ("api/Create")] public HttpResponseMessage Create([FromBody] Contact model) { var c = new Contact(); c.FName = 型号.FName; c.LName = 模型.LName;存储库。添加(c);返回新的 HttpResponseMessage(HttpStatusCode.OK);如果我包含 x-www-form-urlencoded 它会插入一条带有空白字段的记录。如果我排除它,则不会插入记录。
-
编辑您的问题以包含 C# 代码并添加相关标签
-
已解决 $http({ method: 'POST', url: url, headers: {'Content-Type': 'application/x-www-form-urlencoded'}, data: $.param (dataObj) });
标签: angularjs asp.net-web-api insert postman angularjs-http