1

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

ALFA
  • 263
  • 1
  • 5
  • 20
  • what restfull service doe you have? – Tjaart van der Walt Nov 17 '15 at 08:43
  • You could try this: http://stackoverflow.com/questions/33660712 – www.admiraalit.nl Nov 17 '15 at 08:49
  • my Service : WEB API 2 – ALFA Nov 17 '15 at 08:49
  • Is your Method marked as `[HttpPut]`? – Tjaart van der Walt Nov 17 '15 at 08:52
  • 2
    What's unclear about the error message? Your browser is making a preflight OPTIONS request. The server is saying that OPTIONS requests are unacceptable. – Quentin Nov 17 '15 at 08:54
  • 1
    The preflight request mentioned by @Quentin happens as a part of CORS implementation - https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS. Also worth reading what 405 HTTP code means - http://www.checkupdown.com/status/E405.html – tiblu Nov 17 '15 at 08:58
  • function and header in a edited issue . I try to make .config(function ($httpProvider) { $httpProvider.defaults.headers.common = {}; ....}. And I have Failed to load resource: the server responded with a status of 405 (Method Not Allowed) – ALFA Nov 17 '15 at 09:10
  • If OPTIONS is forbidden then trying to strip out extra request headers isn't going to make it any less forbidden (and isn't likely to remove the requirement for preflight handling) – Quentin Nov 17 '15 at 09:12
  • How I will put header to allow my service to authorize the request – ALFA Nov 17 '15 at 09:17
  • sorry I read the tutorial but I can not unlock my problem :) – ALFA Nov 17 '15 at 11:05

1 Answers1

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>
Marc
  • 16,170
  • 20
  • 76
  • 119