I am trying to make a client side click and redirect to another site with header information added, my client side code for the onclick is this:
function selectApp(appGUID, userId ,embedUrl)
{
if(embedUrl==="")
{
var success = setAppGUID(appGUID);
window.location.replace('AppDetail.aspx');
}
else
{
$.ajax({
type: "POST",
url: embedUrl,
contentType: "text/html",
beforeSend: function (xhr, settings) {
xhr.setRequestHeader("UserId", userId);
},
success: function (msg) {
//actually redirect to the site
window.location.replace(embedUrl);
}
});
}
}
And the server side code in embedUrl is
protected void Page_Load(object sender, EventArgs e)
{
string isSet = (String)HttpContext.Current.Session["saveUserID"];
if (String.IsNullOrEmpty(isSet))
{
NameValueCollection headers = base.Request.Headers;
for (int i = 0; i < headers.Count; i++)
{
if (headers.GetKey(i).Equals("UserId"))
{
HttpContext.Current.Session["saveUserID"] = headers.Get(i);
}
}
}
else
{
TextBox1.Text = HttpContext.Current.Session["saveUserID"].ToString();
}
}
This seems to work but it’s not too elegant. Is there a way to redirect with header data? Without (what I’m doing) saving header info in a session var then doing a redirect in 2 seperate pieces.
You can implement a page method and post data to it.
Moving your current logic in that method.
See these examples:
http://encosia.com/using-jquery-to-directly-call-aspnet-ajax-page-methods/
http://www.asp.net/ajaxlibrary/jquery_webforms_Post_Data_To_PageMethod.ashx
Upon successful post you can redirect to another page like your current implementation.
Edit: Try something like this:
1) Add a Web method in Code behind
2) Call it from JS
More on calling Page methods with jQuery: