TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Sachin Singh
7
55.8k
81.4k
How to get Location Header in jquery success in a cross origin request
Sep 3 2020 2:45 AM
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.
Reply
Answers (
2
)
Deserialize JSON with dynamic keys using C# Json.NET
How to show exported excel in toolbar in c#?