2016-02-02 60 views
1

是有一個解決方案布爾響應從PHP交使用angularjs控制器方法獲取!!!angularjs布爾PHP交回調

我嘗試這個解決方案,並從PHP代碼返回布爾類型,它不工作

Code

+3

歡迎來到StackOverflow!請通過http://stackoverflow.com/help/asking。並且請不要將文本/代碼發佈爲圖片... –

回答

1

我假設你是outputing字符串「true」,「返回」從PHP的反應,例如

echo "true"; 

在這種情況下,要使用的服務器將內容類型http頭,這可能是文本/無格式在這種情況下。 ('Content-Type:application/json');可以設置 header('Content-Type:application/json');

true應該是有效的JSON值。 Hovewer使用application/json內容爲您提供更多選擇。

您可以使用此方法發送整個對象!但我會留給你的。

通過使用正確的Content-Type,您的布爾值應該是automaticaly proccesed。

在JavaScript方面,檢查你的response.data應該就好了。

Hovewer,如果你不想在你的php添加內容類型標題,你應該寫你的病情是這樣

if(response.data == "true") //or better use === 

爲什麼?因爲現在你可能正在評估字符串 - 這總是正確的。

我希望我能幫到你,因爲如果你沒有發佈你的php代碼很難回答,並更好地解釋問題。

0

你不能直接布爾值作出響應,在這種情況下,你可以發送包含只是這樣迴應一個JSON對象:

服務器端:

<?php 

// inform the client about the content-type of this response in order to parse it automatically 
header('Content-Type: application/json'); 

// send a json encoded array 
echo json_encode(array(
    "boolean_value" => true, 
    "other_data" => "something else if you want..." 
)); 

?> 

客戶端:

... 
function(response){ 
    if(response.data.boolean_value == true){ 
    ... 
    }else{ 
    ... 
    } 
} 
...