Search code examples
angularjsresthttpput

ANGULAR $http.put REST


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.
enter image description here

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


Solution

  • 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>