【问题标题】:jQuery Ajax call to not expect any resultjQuery Ajax 调用不期望任何结果
【发布时间】:2013-07-09 07:27:11
【问题描述】:

我对@9​​87654321@有点陌生。

我一直在使用$.post.. 发送和接收消息,但是它极大地改进了我的网站。

我需要一个工具来将 ajax cal 发送到服务器进行验证

如果验证成功,那么我希望服务器重定向 如果没有,它应该返回页面并显示错误消息。

我的问题是哪个jQuery 电话最适合这个。

根据我的经验,当我使用$.post('....') 并在服务器上重定向时,由于某种原因它不会重定向。

【问题讨论】:

    标签: php jquery ajax redirect


    【解决方案1】:

    您可以在 ajax 调用中使用另一个 ajax 调用,或者只是触发表单提交。

    $("form").on("submit", function (e) {
        var $this = $(this);
        e.preventDefault();
        $.get("validate.php", $this.serialize()).done(function (valid) {
            if (valid) {
                $this.trigger("submit");
                //OR
                $.post("submit.php").done(function () { window.location = next; });
            }
        });
    });
    

    【讨论】:

    • 在您的场景中,重定向是否在 jQuery 中触发?
    【解决方案2】:

    如果您使用 header() 使用 PHP 进行重定向,它不会使用 ajax 进行重定向,因为内容已经发送到客户端(您只能在打磨任何数据之前使用此功能)

    您应该尝试通过 javascript 重定向,而不是使用 window.location.href="my new page"

    您可以在以下链接中找到更多信息

    http://ntt.cc/2008/01/21/5-ways-to-redirect-url-with-javascript.html http://php.net/manual/en/function.header.php

    如果你没有使用 PHP 重定向,那么我很抱歉

    【讨论】:

      【解决方案3】:

      你可以使用$.post的底层函数$.ajax

      $.ajax({
                          url: 'url',
                          data: 'json_or_string',
                          type: 'POST',
                          dataType: 'json',
                          success: function(json) {
                              if (json.clear == 1) {
                                                  document.location.href = "redirect";
                              } else {
                              $('#errormessage').html('Error Validation');    
                              }
      
                          },
                          error: function (error) {
      
                          },
      
      
                      });
      

      【讨论】:

        【解决方案4】:

        JS:

        $('#submitButtton').click(function(){
            $.ajax({
                dataType: "json",
                type: 'POST',
                url: url/to/ajax-results.php',
                data: { $('form#my-form').serialize(); }
            }).done(function(data){
                if(data.response == 'error') {
                    $('#results').html('<div class="error">'+data.report+'</div>');
                    $('#results').show();
                    return false;
                }
        
                window.location.href = data.redirect
            });
        });
        

        PHP:

        <?php
        $errors = array();
        
        $val1 = $_POST['val1'];
        if(empty($val1)) {
            $errors[] = 'val1 cannot be empty';
        }
        
        if(count($errors) > 0) {
            echo json_encode(array(
                'response' => 'error',
                'report' => 'send your errors array, or custom error message'
            ));
            exit();
        } else {
            echo json_encode(array(
                'response' => 'ok',
                'redirect' => 'http://www.domain.com/url/to/redirect'
            ));
            exit();
        }
        
        ?>
        

        【讨论】:

        • 这个答案对我来说更清楚,但是重定向可以在 php 端完成,而不是将重定向位置发送到 JS 吗?
        • 简短回答“否”。为什么 JS 重定向不适合你?
        猜你喜欢
        • 2018-01-09
        • 1970-01-01
        • 1970-01-01
        • 2015-11-10
        • 2016-09-12
        • 1970-01-01
        • 2011-05-22
        • 2011-07-13
        • 2013-04-01
        相关资源
        最近更新 更多