trying to redirect from an ashx page to an aspx page

夙愿已清 提交于 2019-12-25 11:25:36

问题


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.



来源:https://stackoverflow.com/questions/17866856/trying-to-redirect-from-an-ashx-page-to-an-aspx-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!