问题描述:

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

网友答案:

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.

相关阅读:
Top