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);
- }
- }
fiddler successfully recognized the location
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);
- }
but no luck , getting null in alert.