2014-06-25 127 views
0

我有一个基本的ajax注释表单,带有textarea和yes/no radio对象。request.send()之后重定向 - 请求未发布,页面重定向

  • 如果“是”选择的,用户的评论发布,然后 他们被带到一个新的页面。
  • 如果“否”,则发布评论并保留在同一页面上。

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

这里的后处理脚本:

// 
    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()被解雇后,关闭?似乎重定向正在打破这一部分。

+0

在处理请求结果的回调函数中执行重定向。 – CBroe

回答

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; 
    } 
}