【问题标题】:I get "Get" web service instead of "POST" web service我得到“获取”网络服务而不是“发布”网络服务
【发布时间】: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


【解决方案1】:

在您的 api 中尝试删除 [Route("api/Students/ajout")] 而不是 [System.Web.Http.ActionName("Create")] 放置 [HttpPost]。 在您的控制器中,我建议您使用 angularjs 查询而不是 ajax 请求。

如果你编辑你的 api 控制器,这个请求应该运行:

var request = $http({
    url:"api/students/",
    dataType:"json", 
    method:"POST",
    data:JSON.stringify(student),//you can send directly the object student if is well-trained or use params:{'name':value, ...}
    contentType:"application/json; charset=utf-8"
}); 

如果您使用 JSON.stringify,请在您的 Api 控制器中恢复您的对象:

 [HttpPost]
    public void CreateStudent([FromBody]Student value)
    {
      ...
    }

我不知道你是否这样做,但在你的 html 中,而不是在你的输入上绑定属性 firstname,绑定 student.firstName。像这样,您可以使用属性 $scope.student.firstName 直接恢复 $scope.student,而不是逐个恢复每个属性。所以像我在例子中那样直接发送对象。

所以我希望这会有所帮助。 让我了解你的进展。祝你有美好的一天。

【讨论】:

  • 在你的 api 控制器上,在你的类名称上方添加 '[Route("api/[controller]")]' 像这样在你的请求中你不再需要添加完整的 url,只需使用“api/students”。
猜你喜欢
  • 1970-01-01
  • 2013-07-28
  • 2014-12-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-03
  • 2012-09-17
  • 1970-01-01
  • 2014-03-04
相关资源
最近更新 更多