2012-03-26 92 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'); 
      } 
    });