2013-06-18 61 views
-1

我在我的應用程序中遇到了一個大問題。我有一個連接到php webservice的應用程序,並在按下按鈕時獲取一些數據。唯一的問題是,我的服務沒有迴應我,我找不到原因。Javascript調用php web服務器

我想發送'Windesheim'到我的服務器,並且服務器(現在硬編碼)返回'Windesheim'。我沒有使用該陣列進行測試。我正在使用谷歌瀏覽器。

這是我的.html:

<html> 
<head> 
    <title>Ekiden</title> 
    <script src="http://code.jquery.com/jquery-1.8.2.js"></script> 
    <script type="text/javascript" src="_Script.js"></script>  
</head> 
<body> 
    <input id="team" type="text"/><input id="button" type="button" value="Go" /> 

    <div id="feedback"></div> 
</body> 
</html> 

這是我的.js:

$('#button').click(function() { 
var team = $('#team').val(); 

$.POST('_Dataserver.php', {team: team }, function(data) { 
    $('#feedback').text(data); 
}); 
}); 

這是我.PHP:

<?php 
header('Content-Type: application/json'); 

$teams = array 
(
"Windesheim"=>array 
    (
     // Naam, Leeftijd, Tijd, Afstand, Gemiddelde snelheid 
     1 => array ('Alfred', 20, 0, 0, 0), 
     2 => array ('Willem', 36, 0, 0, 0), 
     3 => array ('Michael', 22, 0, 0, 0), 
     4 => array ('Stefan', 27, 0, 0, 0), 
     5 => array ('Timmy', 20, 0, 0, 0), 
     6 => array ('Pascal', 20, 0, 0, 0) 
    ), 
"Deltion"=>array 
    (
     // Naam, Leeftijd, Tijd, Afstand, Gemiddelde snelheid 
     1 => array ('Simon', 21, 0, 0, 0), 
     2 => array ('Manuel', 33, 0, 0, 0), 
     3 => array ('Stephan', 29, 0, 0, 0), 
     4 => array ('Marko', 42, 0, 0, 0), 
     5 => array ('Nick', 23, 0, 0, 0), 
     6 => array ('Achmed', 20, 0, 0, 0) 
    ), 
"Landstede"=>array 
    (
     // Naam, Leeftijd, Tijd, Afstand, Gemiddelde snelheid 
     1 => array ('Patrick', 20, 0, 0, 0), 
     2 => array ('Mohammed', 18, 0, 0, 0), 
     3 => array ('Wilson', 19, 0, 0, 0), 
     4 => array ('Walie', 22, 0, 0, 0), 
     5 => array ('Marco', 52, 0, 0, 0), 
     6 => array ('Mohabie', 45, 0, 0, 0) 
    ) 
); 

// Als er teams worden opgevraagd 
if (isset($_POST['team'])) 
{ 
    $team = $_POST['team']; 
    if($team == 'Windesheim') 
    { 
     //echo json_encode($teams[$team]); 
     echo json_encode('Windesheim'); 
    } 
} 
// Als er niks wordt opgevraagd 
else 
{ 
    echo ''; 
} 

?> 
+0

如何確定 「我的服務沒有響應」? – Quentin

+0

當您嘗試此操作時,Web瀏覽器的控制檯會說什麼? Webkit的JS控制檯提供了關於AJAX請求的非常好的信息,並且還允許您在網絡選項卡中跟蹤它們。確保你的服務器實際上正在被擊中。 –

+0

'json_encode('Windesheim');'的輸出不會是有效的JSON文本。 JSON文本必須有一個對象或數組作爲最外層,而不是一個字符串。 – Quentin

回答

3

你需要用_Scripts.js的代碼中的document.ready功能。

您的代碼在DOM呈現之前執行,因此$('#button')不返回任何元素。

_Scripts.js應該是:

$(document).ready(function() { 
    var team = $('#team').val(); 
    $.POST('_Dataserver.php', {team: team }, function(data) { 
     $('#feedback').text(data); 
    }); 
}); 
+0

有了這個,我有以下錯誤:遺漏的類型錯誤:對象功能(選擇,背景){ \t \t // jQuery對象實際上只是在init構造「加強」 \t \t回報新jQuery.fn。init(selector,context,rootjQuery); \t}沒有方法'POST' –

+0

jQuery返回一個Promise對象,請參閱文檔:http://api.jquery.com/jQuery.post/ – Pinoniq

0
$(document).ready(function() { 
}); 

上面的代碼在你的js代碼中缺少。

此外,

在PHP中,你正試圖從請求/後值檢查值「Windesheim」,

if($team == 'Windesheim') { 
    //echo json_encode($teams[$team]); 
    echo json_encode('Windesheim'); 
} 

但在JS要傳遞的價值爲球隊ID是空的在html輸入類型值爲,

<input id="team" type="text"/> 

所以它不打印/響應任何從客戶端端PHP。

要在HTML解析,請更改值 '' 到 'Windesheim'

(即)<input id="team" type="text" value ="Windesheim"/>

+0

當我填寫我的輸入字段並按下按鈕時,不要設置值嗎? –