Matt Stone
Matt Stone

Reputation: 281

Redirect an asp page to another URL

I have a asp.net form which hasn't action attribute, and the submit will handle by ajax as follow.

$.ajax({
                url: "home2.aspx/DoSubmit",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                data: '{x: "' + $("#x").val() + '",y: "' + $("#y").val() + '"}',
                dataType: "json",
                success: function (result)
                {
                    alert("success...");
                },
                error: function (xhr, status) {
                    alert("Error", status);
                }
            });
            return false;
        });

in code behind after providing related response for ajax call I want to redirect asp.net page to another URL(even different domain) with others Parameters which I had there in backend. I test this method, creating HttpWebRequest but not work.

var postData = (requestParam.ToString());
            var data = Encoding.ASCII.GetBytes(postData);
            HttpWebRequest request;
            try
            {
                request = (HttpWebRequest)HttpWebRequest.Create("https:someDomain");
            }
            catch (UriFormatException)
            {
                request = null;
            }
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;
            Stream postStream = request.GetRequestStream()
            postStream.Write(data, 0, data.Length);
            postStream.Flush();
            postStream.Close();

this cause this error: Authentication failed because the remote party has closed the transport stream.
thanks for your help

Upvotes: 0

Views: 237

Answers (1)

Sam Axe
Sam Axe

Reputation: 33738

The ajax call is OOB (out of band) from the browser. It has absolutely ZERO connection to the browser navigation mechanism. Thus your redirect will not work.

You need to subscribe to the ajax success (and/or failure) event. In that event you will do the redirection client-side via any number of browser navigation methods (eg window.location).

If you need the redirect url to come from the server, then have your action return it as part of the ajax response.

Upvotes: 2

Related Questions