【问题标题】:Page redirect from external php file (in ajax call)从外部 php 文件重定向页面(在 ajax 调用中)
【发布时间】:2015-04-12 10:57:55
【问题描述】:

我有这个我正在使用的 Salesforce“Web-to-Lead”表单(在 IIS 服务器上),并且我在 Ajax 调用中正确验证了它(使用 roscripts.com ajax 验证作为起点...这使用 Mootools 库来执行其 Ajax 功能),以及使用 cURL 发送所有经过验证的数据,但是我希望表单在验证成功后重定向到新页面。每次单击“提交”按钮时都会运行 Ajax 调用,并且要么在页面上显示错误,要么运行验证代码的“else”部分中包含的任何内容。

Ajax 函数:

window.addEvent('domready', function() {
  $('registerForm').addEvent('submit', function(e) {
    new Event(e).stop();
    var log = $('log_res').empty().addClass('ajax-loading');
    this.send({
      update: log,
      onComplete: function() {
        log.removeClass('ajax-loading');
        //adding a header redirect here works, but redirects every time ajax call runs, regardless if validation succeeds or not
      }
    });
  });
});

“动作”php文件函数:

<?php 
if (rule){ //validation errors } 
else { //where all of the logic happens after validation succeeds

//cURL function 

//redirect attempts:
header( 'Location: http://www.place.com'); // doesn 't work

echo '<meta http-equiv="refresh" content="2" ; url="http://www.place.com">'; //resets the form, but doesn't redirect to www.place.com 
};
?>

我在这里完全被难住了,无论我多么努力,我都无法重定向这个该死的东西。任何帮助将不胜感激!

【问题讨论】:

  • 两种情况下的回报是多少?只需在 oncomplete 中添加一个结果即可知道验证是否成功,如果成功,则重定向。
  • Ajax 调用的文件中的重定向只是在服务器端重定向,并将第二个文件的输出返回给您的 javascript 回调。它不会重定向用户正在查看的页面。

标签: javascript php ajax redirect iis


【解决方案1】:

根据我对 MooTools AJAX 的了解,onComplete 处理程序将在 AJAX 完成时触发——即使出现错误。这对于删除您的加载消息可能很好,但您可能希望使用onSuccess 来处理成功的处理程序,使用onFailure 来处理失败的请求。虽然您的脚本似乎没有直接使用 MooTools,但 MooTools AJAX 就是这样工作的。

请参阅“阿贾克斯!”以http://mootools.net/ 部分为例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多