2015-01-17 130 views
2

我有2個文件time.html和time.php。我試圖在time.html中顯示服務器日期和用戶日期(我使用JS)。在PHP中創建隨機數從1到5,如果數量等於或大於3,PHP應該等待5秒,然後提示錯誤404PHP導致錯誤404不起作用

注意:如果隨機小於3一切完美

PHP:

<?php 

$rnd = rand(1, 5); 
if ($rnd >= 3) { 
sleep(5); 
header("HTTP/1.0 404 Not Found"); 
} 
if ($rnd < 3) { 
    sleep(3); 
    $tz = date_default_timezone_get(); 
    date_default_timezone_set($tz); 
    $time = date('d.m.Y H:i:s'); 
    echo "Timezone: " . $tz . ". On date: " . $time; 
} 
?> 

的jQuery:

$(document).ready(function() { 
      $("button").click(function() { 
       $.ajax({ 
        type: 'POST', 
        url: 'time.php', 
        success: function (data) { 
         $("span").text(data); 
        } 
       }); 
       document.getElementById("jsTime").innerHTML += Date(); 
      }); 
     }); 

回答

1

這是因爲,當服務器錯誤響應,jquery.ajax運行誤差函數,這意味着你的成功函數做ñ ot run。看到錯誤的api.jquery.com/jquery.ajax/#jQuery-ajax-settings

$(document).ready(function() { 
 
      $("button").click(function() { 
 
       $.ajax({ 
 
        type: 'POST', 
 
        url: 'time.php', 
 
        success: function (data) { 
 
         $("span").text(data); 
 
        }, 
 
        error: function(jqxhr){ 
 
         //redirect to 404 page on 404 error 
 
         if(jqxhr.status == 404){ 
 
         window.location.href="HTTP/1.0 404 Not Found"; 
 
         } 
 
        } 
 
       }); 
 
       document.getElementById("jsTime").innerHTML += Date(); 
 
      }); 
 
     });

+0

沒有,這是行不通的。當隨機數大於或等於3時,我的html文件看起來一樣,它什麼都不做 – mihcc

+0

@mihcc,這是因爲當服務器響應一個錯誤時,jquery.ajax運行錯誤函數,這意味着你的成功函數不運行。 看到http://api.jquery.com/jquery.ajax/#jQuery-ajax-settings – Ozan

+0

上的錯誤,所以我應該添加類似於: 錯誤:function(){ //我應該在這裏放什麼?; }); } – mihcc

0

嘗試將數據返回到jQuery的數組一樣

  1. $海峽[0] =真/假;
  2. $ str [1] =「HTTP/1.0 404 Not Found」/「時區:」。$ tz。 「在日期:。」 $時間

在成功檢查車況

success: function (data) { 
if(data[0]==true){ 
$("span").text(data[1]); 
    }else{ 
window.location.href="HTTP/1.0 404 Not Found"; 
} 

我是這麼認爲的Ajax調用必須完成重定向,上面的代碼在我的機器

1

至於說工作,當服務器返回一個錯誤並且請求失敗時,Jquery Ajax讓你用error函數處理這個錯誤;

嘗試添加幾個選項更多設置:

  • 添加error功能。
  • 添加statusCode對象並與之一起玩;這使您可以對錯誤處理進行更多結構化的控制。

所以:

$("button").click(function() { 
    $.ajax({ 
     type: 'POST', 
     url: 'time.php', 
     success: function (data) { 
      console.info('success'); 
      $("span").text(data); 
     }, 
     error: function (jqXHR, textStatus, errorThrown) { 
      console.info(jqXHR.responseText); 
      console.info(jqXHR.status); 
      console.info(textStatus); 
      console.info(errorThrown); 
      $("span").text(jqXHR.responseText); 
     }, 
     statusCode: { 
      404: function() { 
       alert("page not found"); 
      } 
     } 
    }); 
    document.getElementById("jsTime").innerHTML += Date(); 
}); 

和修改,如果裏面的PHP文件:

if ($rnd >= 3) { 
    sleep(1); 
    echo "my response text"; 
    header("HTTP/1.0 404 Damn, Not Found"); //just for playing, then keep with HTTP standard description 
} 

引述jQuery.ajax文檔:

error

Type: Function(jqXHR jqXHR, String textStatus, String errorThrown) A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn.

Note:This handler is not called for cross-domain script and cross-domain JSONP requests.