【问题标题】:How to return ajax error if url IP is incorrect如果 url IP 不正确,如何返回 ajax 错误
【发布时间】:2016-09-27 18:03:45
【问题描述】:

我一直在尝试让 ajax 为域 IP 不正确的发布请求返回错误。

    var serverAddress = "192.168.24.58"; // my current local host ip

    $.ajax({
        method: "POST",
        url: "http://"+serverAddress+"/electronTestPage/process.php",
        data: "action=testAjax",
        success: function(data) {

            alert("SUCCESS!!" + data);
        },
        error: function(jqXHR, exception) {
            if (jqXHR.status === 0) {
                alert('Not connect.\n Verify Network.');
            } else if (jqXHR.status == 404) {
                alert('Requested page not found. [404]');
            } else if (jqXHR.status == 500) {
                alert('Internal Server Error [500].');
            } else if (exception === 'parsererror') {
                alert('Requested JSON parse failed.');
            } else if (exception === 'timeout') {
                alert('Time out error.');
            } else if (exception === 'abort') {
                alert('Ajax request aborted.');
            } else {
                alert('Uncaught Error.\n' + jqXHR.responseText);
            }
        }
    });

如果我将其更改为 var serverAddress = "192.168.24.581111" 它不会抛出任何错误。

但是,如果我更改 "http://"+serverAddress+"/1111electronTestPage/process.php" 我会得到正确的 404 错误。

有什么简单的方法可以让 ajax 在不 ping ip 的情况下在错误/不存在的 IP 上引发错误?

感谢任何建议。

【问题讨论】:

  • 你有没有检查过在这种情况下是否调用了complete 回调?
  • 我已经检查了完整但没有任何响应
  • 更具体地说,当 ip 正确时有一个“完整”响应,当 ip 不正确时根本没有响应
  • 您使用的是什么浏览器和浏览器版本?什么版本的jquery?我已经用主机名“192.168.24.581111”和几个当前的浏览器(Firefox、Chrome、Edge、IE)对此进行了测试,它总是显示“未连接。验证网络。”。当浏览器等待超时以解析主机名时,消息显示有时需要长达 2 分钟。
  • 根据您的建议,我等了大约 1 分钟,然后超时并返回错误。这回答了我想知道是否有将超时计时器缩短到 5 秒之类的东西。谢谢你的建议。

标签: javascript jquery ajax post ip


【解决方案1】:

调用error 回调。但根据本地网络设置,使用无效主机名可能需要两分钟,直到浏览器意识到主机名无效并调用 error 回调。

您可以通过在 JQuery 中的 ajax 调用中指定 timeout 参数来缩短浏览器等待失败的时间。详情请见this answerthis question

【讨论】:

    【解决方案2】:

    重要的是要知道,有时通过 ajax 工作的不同类型的响应可能过于混乱和乏味,这就是为什么有不同的方法从 PHP 和 AJAX 和 JSON 返回响应。

    最简单的方法是,如果你的 PHP 文档正在分析结果,你可以通过一个数组和一个 json_encode 响应返回并在回复中执行 PARSE AJAX。

    瞬间:

    <script type="text/javascript">
        var new_ip = '127.0.0.0';
        var parameters = 'action=check_ip&ip='+new_ip;
        $.ajax({
            method:'POST',
            data:parameters,
            url:'myfile.php',
                success:function(response) {
                    var answer = JSON.parse(response);
                    switch( answer.status_response ) {
                        case 'success' :
                            alert(''+answer.message_response+'');
                            break;
                        case 'error' :
                            alert(''+answer.message_response+'');
                            break;
                    }
                }
        });
    </script>
    
    <?php 
    
        //myfile.php
        $action = $_POST['action'];
        switch ( $action ) {
            case 'check_ip':
                $ip = $_POST['ip'];
                $theip = '192.168.1.254';
                if ( $ip == $theip ) {
                    $response = array(
                                     'status_response'  => 'success',
                                     'message_response' => 'The IP is correct');
                    echo json_encode($response);
                }
    
                else {
                    $response = array(
                                     'status_response'  => 'error',
                                     'message_response' => 'The IP is incorrect');
                    echo json_encode($response);                
                }
                break;
        }
    
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      • 2014-04-19
      • 2019-07-07
      相关资源
      最近更新 更多