【发布时间】:2015-07-07 08:31:58
【问题描述】:
我正在尝试创建一个简单的页面,我在其中使用 $http.put 将一些数据发送到服务器。
脚本代码
<script>
var myApp = angular.module("myApp", []);
myApp.controller("HttpPostController", function ($scope, $http) {
$scope.SendHttpPostData = function () {
var data = $.param({
firstName: $scope.firstName,
lastName: $scope.lastName,
age : $scope.age
});
$http.put('/ServerRequest/PutDataResponse', data)
.success(function (data, status, headers, config) {
$scope.ServerResponse = data;
})
.error(function (data, status, header, config) {
$scope.ServerResponse = htmlDecode("Data: " + data +
"\n\n\n\nstatus: " + status +
"\n\n\n\nheaders: " + header +
"\n\n\n\nconfig: " + config);
});
};
});</script>
我的 HTML 代码
<div ng-app="myApp" ng-controller="HttpPostController">
<form ng-submit="SendHttpPostData()">
<p>First Name: <input type="text" name="firstName" ng-model="firstName" required /></p>
<p>Last Name: <input type="text" name="lastName" ng-model="lastName" required /></p>
<p>Age : <input type="number" name="age" ng-model="age" required /></p>
<input type="submit" value="Submit" />
<hr />
{{ ServerResponse }}
</form></div>
ASP.NET MVC 控制器操作方法
[HttpPut]
public ContentResult PutDataResponse(string firstName, string lastName, int age)
{
return Content("First name: " + firstName +
" | Last name: " + lastName +
" | Age: " + age +
" | Request Type: " + Request.RequestType.ToString());
}
我收到的错误消息如下
TTP 错误 404.0 - 未找到 您要查找的资源已被删除、更改名称或暂时不可用。最可能的原因: Web 服务器上不存在指定的目录或文件。 URL 包含印刷错误。自定义过滤器或模块(例如 URLScan)限制对文件的访问。您可以尝试的方法: 在 Web 服务器上创建内容。查看浏览器 URL。检查失败的请求跟踪日志,看看哪个模块在调用 SetStatus。
所以在 ASP.NET MVC 中它不起作用。 (POST 和 GET 有效)。我用 ASP.NET MVC Web API 尝试了上述方法,它工作正常。我可以使用 mvc web api 的 PUT 方法发送和接收响应。
问题是为什么它在 ASP.NET MVC 中不起作用,即使方法动词是 [HttpPut]。
谢谢
【问题讨论】:
-
如果您在浏览器开发人员工具的“网络”选项卡中检查请求会是什么样子?
-
数据:IIS 8.0 详细错误 - 404.0 - 未找到。阅读我上面帖子的最后一部分(斜体)。
-
感谢 Ric,但现在情况并非如此。 ASP.NET MVC Web API 现在允许 HttpPut 请求类型,正如我在上面的帖子中提到的,这里是示例 techfunda.com/Howto/angularjs/573/http-put-server-request 这个问题只有在我们使用 ASP.NET MVC 控制器时才会出现。
-
我的意思是发送请求时使用的 url。我可以清楚地看到您的问题文本中的错误。
标签: asp.net-mvc angularjs