ANGULAR $http.put REST

心已入冬 提交于 2019-12-11 10:25:32

问题


I try to consume a Web service with REST angular ($ http). The GET is OK, but when I try to make a change with $ http.put, the error appears below:

XMLHttpRequest cannot load http://10.2.150.238:90/api/Collaborateurs/1. Response for preflight has invalid HTTP status code 405.

update function :

.factory('UpdateCollaborateur', function ($http, config) {
    var fac = {};
    fac.Update = function (matricule,collaborateur) {
        return $http.put(config.apiUrl + 'Collaborateurs/'+ matricule, collaborateur);

    };
    return fac;
})

WEB API 2 , put :

 public IHttpActionResult PutCollaborateur(int id, Collaborateur collaborateur)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != collaborateur.matricule_collaborateur)
            {
                return BadRequest();
            }

            db.Entry(collaborateur).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CollaborateurExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

Header of my fonction :

 [ResponseType(typeof(void))]

What is this problem?? Thanks


回答1:


405 means that the method is not allowed

If you use [HttpPut] make sure that's your webconfig.xml allows it

Try the following

<system.webServer>
    <handlers>
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>


来源:https://stackoverflow.com/questions/33752356/angular-http-put-rest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!