trying to redirect from an ashx page to an aspx pa

2019-09-05 02:16发布

问题:

I have been trying to redirect to an aspx page along with a QueryString through an Ajax call but even thought the handler is called the redirect does not take place.

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/plain";
    string searchValue = context.Request["txtBoxValue"].ToString();
    context.Response.Redirect("SearchResults.aspx?search=" + searchValue);

}

 $.ajax({
url: 'Handlers/SearchContent.ashx',
data: { 'txtBoxValue': txtBoxValue },
success: function (data) {
}

});

Any advice perhaps as to why the transfer does not take place and how to do this

kind regards

回答1:

Since you are doing an ajax request clearly the Redirect should have no effect. What you need to do instead is do it from the client-side, on the success handler:

public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/plain";
    string searchValue = context.Request["txtBoxValue"].ToString();
    //Return the redirect URL instead
    context.Response.Write("SearchResults.aspx?search=" + searchValue);     
}



$.ajax({
    url: 'Handlers/SearchContent.ashx',
     data: { 'txtBoxValue': txtBoxValue },
      success: function (data) {
         window.location= data;//redirect here. "data" has the full URL
    }
});

Now, if this is all you are doing in the ashx handler, I don't really see the need for the ajax request.