【发布时间】:2016-01-01 18:59:18
【问题描述】:
我已经使用这篇帖子Jquery Ajax Posting json to webservice 通过“POST”网络服务向服务器发送数据,但它的特征类似于“Get” 在浏览器的“网络”部分,这是我得到的:
这是我的网络服务代码(ASP.net):
// Insert Student
[Route("api/Students/ajout")]
[System.Web.Http.ActionName("Create")]
public async Task<HttpResponseMessage> Post(Student value)
{
try
{
if (ModelState.IsValid)
{
_StudentService.Insert(value);
var response = Request.CreateResponse<Student>(HttpStatusCode.Created, value);
await _unitOfWorkAsync.SaveChangesAsync();
return response;
}
else
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, "Model state is invalid");
}
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}
这是我的代码(AngularJS)
$.ajax({
type: 'POST',
url : 'http://localhost:50001/api/Students/ajout',
dataType: 'jsonp',
contentType: "application/json; charset=utf-8",
data: { FirstName: ''+$scope.FirstName+'', LastName: ''+ $scope.LastName+'' , Email: ''+ $scope.Email+'' , DropOut:'' +$scope.dropout.value1+'' , Live: ''+$scope.live.value2+'' , ClassId:2, ImageId:1}, // On fait passer nos variables, exactement comme en GET, au script more_com.php
success: function(data){
console.log("success");
},
failure: function(errMsg) {
console.log(errMsg);
}
});
请问您知道我该如何处理这个问题,非常感谢您的帮助
更新: 我已经改变了这样的代码:
$.ajax({
type: 'POST',
url : 'http://localhost:50001/api/Students/ajout',
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: { FirstName: ''+$scope.FirstName+'', LastName: ''+ $scope.LastName+'' , Email: ''+ $scope.Email+'' , DropOut:'' +$scope.dropout.value1+'' , Live: ''+$scope.live.value2+'' , ClassId:2, ImageId:1}, // On fait passer nos variables, exactement comme en GET, au script more_com.php
success: function(data){
$rootScope.usersData = angular.toJson(data);
console.dir($rootScope.usersData);
console.log("success");
},
failure: function(errMsg) {
console.log(errMsg);
} });
但我收到此错误: XMLHttpRequest 无法加载 http://localhost:50001/api/Students/ajout。预检响应包含无效的 HTTP 状态代码 405
【问题讨论】:
-
我相信你不能通过 jsonp POST
-
这是 jquery 代码而不是 angularjs
-
感谢 mxa055 的评论我的意思是 angularJS 我将此请求放入控制器中:D
-
如果您需要在控制器中发布 get 请求,请使用 angularjs $http 并且 angularjs 标记将是合适的,或者如果您这样保留它,请删除 angularjs,因为它与您的无关问题。
标签: asp.net angularjs web-services rest