2012-03-26 32 views
0

考慮以下幾點:從AJAX'd PHP腳本返回/過濾值的正確方法?

<?php 
//daytime_check.php 
    $is_daytime = false; 
    if ($is_daytime) { 
     echo '1'; 
    } else { 
     echo '0'; 
    } 
?> 

===================================== =============================================

// javascript/jQuery 
$.ajax({ 
    type: 'POST', 
    url: 'daytime_check.php', 
    success: function(response) { 
     if(response == false) { 
      alert('Goodnight, brother'); 
     } else { 
      alert('Day\'s a wastin, brother'); 
     }    
    }, 
    error: function() { 
     //handle error 
    } 
}); 

這是我迄今爲止處理來自AJAX'd PHP腳本的響應。我希望有人能以更好的方式給我一些提示,因爲這種方法感覺很笨拙。

特別笨重的是處理JS腳本輸出的「過濾」。例如:

在這種情況下,來自PHP的響應將是JS var response ='0'。現在人們不能簡單地在JS中使用if (!response)...來過濾,因爲顯然!response評估爲false,而有趣的是,response == false評估爲true。我想,與玩雜耍有關。

由於我從PHP返回的東西的唯一方法是在文本(echo語句)中,當我到達JS端時,我無法返回正確的真/假值來過濾。有沒有更好的方法來處理這個問題?

我希望這至少有一點意義。

回答

1

您可以返回任何您想要的類型。只需使用JSON響應。

// you can return what ever you want via JSON 
die(json_encode(array(
    'isDateTime' => $is_datetime, 
    'message' => 'Some optional message to display', 
    'timestamp' => time() 
))); 

這將輸出字符串:

{"isDateTime":false,"message":"Some optional message to display","timestamp":1332792739} 

而且在客戶端jQuery將解析此響應:

$.ajax({ 
    type: 'POST', 
    url: 'daytime_check.php', 
    dataType: 'json', 
    success: function(response) { 
     if (response.isDateTime) { ... } 
     // typeof response.isDateTime == 'boolean' 
     // alert(response.message)   
    }, 
    error: function() { 
     //handle error 
    } 
}); 
+0

我喜歡這樣。另外有趣的是,當從PHP返回值時,如何使用'die()'。這只是爲了消除內存問題的腳本? – eysikal 2012-03-27 19:40:28

+1

那麼你可以使用'echo'或'print',不管。我更喜歡'死',因爲這樣我可以確定我只輸出JSON,之後腳本將關閉。 – dfsq 2012-03-27 20:53:30

+0

我喜歡這個想法。我以前沒看過它。似乎很有道理。 – eysikal 2012-03-27 20:57:14

0

如果您只需要在成功處理程序中顯示消息,那麼您爲什麼不返回消息本身?

<?php 
//daytime_check.php 
    $is_daytime = false; 
    if ($is_daytime) { 
     echo "Day's a wastin', brother"; 
    } else { 
     echo "Goodnight, brother"; 
    } 
?> 

$.ajax({ 
    type: 'POST', 
    url: 'daytime_check.php', 
    success: function(response) { 
     alert(response);   
    }, 
    error: function() { 
     //handle error 
    } 
}); 
+0

那麼,這是過於簡單的代碼,例如/簡潔的目的。在這種情況下,你所建議的會很好。但我正在尋找處理所有情況的東西。 – eysikal 2012-03-26 20:16:16

+0

返回'1'或'0'很好。您可以檢查將通過任何非零或非空值或非空值的「if(response)」。 – ShankarSangoli 2012-03-26 20:18:38

+0

你能澄清嗎?當'response'實際上指向'true/false'的字符串表示形式爲''0''或'「1''時,肯定檢查'if(response)'' – eysikal 2012-03-26 22:21:11

0

這裏是由智能前同事提供了一個巧妙的解決辦法。從你的PHP腳本,通過以下方式

返回值:

<?php 
    // To return an error 
    echo json_encode(
     array(
      'success' => false, 
      'message' => '[error message here]', 
     ) 
    ); 
    // To return a successful response 
    echo json_encode(
     array(
      'success' => true, 
      'response' => [return value here], 
     ) 
    ); 

通過這種方式,我們可以很容易做到的JS端邏輯:

$.ajax({ 
    type: 'POST', 
    url: 'ajax_example.php', 
    success: function(response) { 
       response = JSON.parse(response); 
       if (!response.success) { 
        alert(response.message); 
       } else { 
        console.log(response.response);  
       } 
    }, 
    error: function() { 
       //handle error 
       alert('error doing ajax, mate'); 
      } 
    });