request.send()后重定向 - 请求未发布,页面重定向

时间:2014-06-25 21:00:15

标签: javascript ajax redirect

我有一个带有textarea和yes / no无线电对象的基本ajax注释表单。

  • 如果是,那么'如果已选中,则会发布用户的评论 他们被带到一个新的页面。
  • 如果' no',则会发布评论并将其保留在同一页面上。

当用户选择“是”时,不会发布评论,并且页面会重定向。

这里是帖子脚本:

    //
    flag_yesno      = getRadioValue('form_comments', 'flag_yesno');

    request.open('POST', 'process.php?t=' + new Date().getTime());
    request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

    request.onreadystatechange = function()
    {
        if(request.readyState == 4)
        {
            // put response text in the comment_posts div
            comment_posts.innerHTML     = request.responseText;
        }
    }

    // set up POST parameters
    var params = "&foo=" + foo + "&bar=" + bar;

    // send it on over!
    request.send(params);

    // redirect if needed
    if (flag_yesno=='yes')
    {
        window.location = '/foo.php?foo='+foo;
    }

request.send()被解雇后,有没有办法处理重定向?似乎重定向正在打破这一部分。

1 个答案:

答案 0 :(得分:2)

等到通话结束。等待"直到它被解雇"很难衡量。

if(request.readyState == 4)
{
  if (flag_yesno == 'yes')
  {
    // redirect if needed
    window.location = '/foo.php?foo='+foo;
  } 
  else
  {
    // put response text in the comment_posts div
    comment_posts.innerHTML     = request.responseText;
  } 
}
相关问题