the web api post method is setting location header like below
- public HttpResponseMessage Post([FromBody]Employee emp)
- {
- try
- {
- db.Employees.Add(emp);
- db.SaveChanges();
- var msg = Request.CreateResponse(HttpStatusCode.Created, emp);
- msg.Headers.Location = new Uri(Request.RequestUri +"/"+ emp.Id.ToString());
- return msg;
- }
- catch
- {
- return new HttpResponseMessage(HttpStatusCode.InternalServerError);
- }
- }
Its a cross origin request , so xhr doesn't include all headers untill you force it to set additional headers in "Access-Control-Expose-Headers" , so i had to set this like below
- $.ajax({
- "type": "Post",
- "url": myUrl,
- "dataType": "json",
- "contentType": "application/json;charset=utf-8",
- "data": JSON.stringify(model),
- "traditional": true,
- "dataTpe": "Json",
- "headers": {
- "Access-Control-Expose-Headers": "location",
- },
- "cache": false,
- "crossDomain": true,
- "success": function (data,status,xhr) {
- debugger;
- alert(xhr.getResponseHeader('Location'));
- },
- "error": function (request, message, error) {
- handleException(request, message, error);
- }

Sachin SinghPosted Sep 3, 2020, 9:34 AM
By default, the browser does not expose all of the response headers to the application. The response headers that are available by default are:
- Cache-Control
- Content-Language
- Content-Type
- Expires
- Last-Modified
- Pragma
In order to include other headers we have to set the set the exposedHeaders parameter of [EnableCors].Nisha RegilPosted Sep 3, 2020, 5:32 AM