Redirect To Another Site With Header Information Attached Javascript

Posted by Nick LaMarca on Stack Overflow See other posts from Stack Overflow or by Nick LaMarca
Published on 2012-10-12T15:35:47Z Indexed on 2012/10/12 15:36 UTC
Read the original article Hit count: 177

Filed under:
|

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) {

                           //go to slx
        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 its not too elegant. Is there a way to redirect with header data? Without (what Im doing) Saving header info in a session var then doing a redirect in 2 seperate pieces.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about ASP.NET