【发布时间】:2018-05-23 01:54:27
【问题描述】:
我使用 angularJS + asp.net 进行发布请求,但 Request.Form 始终为空。这是我的代码
索引.cshtml
<div ng-app="phone" ng-controller="phoneCtrl">
<input type="text" ng-model="phoneNumber">
<button ng-click="sendSMS()"> Submit </button>
<br>
{{response}}
{{error}}
</div>
@section scripts {
<script>
var app = angular.module('phone', []);
app.controller('phoneCtrl', function ($scope, $http) {
$scope.sendSMS = function () {
var params = {
phone : $scope.phoneNumber
};
var config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
}
};
$http.post('Home/sendSMS',params , config)
.then(function (response, status, headers, config) {
$scope.response = response.data;
})
.catch(function (data, status, headers, config) {
$scope.error = data;
});
};
});
</script>
}
HomeController.cs
[HttpPost]
public IActionResult SendSMS()
{
System.Diagnostics.Debug.WriteLine("The form has " + Request.Form);
return Content("Received");
}
Request.Form 始终为空,但我可以在 post 请求的请求正文中看到电话条目,所以我想知道我是否在 .net 中提取了错误的数据
【问题讨论】:
-
您是否尝试过使用邮递员之类的帖子?也在你的控制器中,尝试
SendSMS(string phone)和SendSMS([FromBody]string phone),并检查你是否得到了手机的值 -
@NevilleNazerane 尝试在 SendSMS 中添加参数,收到此错误“不支持的媒体类型 - 服务器拒绝为请求提供服务,因为请求的实体采用请求的资源不支持的格式请求的方法。”
-
是的,如果您添加
[FormBody]会收到该错误,除非您以它期望的确切格式 (json) 发送。在这种情况下,我们知道这不是发送的类型 -
您为什么要使用 Request.Form?还是您只需要数据?
-
@NevilleNazerane 我只需要邮寄的电话数据,georgeawg 添加的链接解决了问题
标签: c# asp.net angularjs asp.net-mvc