2011-11-21 234 views
0

我有下面的代碼,並試圖通過使用JQuery和Ajax的GET HTTP請求傳遞數據。當在文本框中輸入「Example」並點擊「Go」時,我希望「example」回到反饋div中。沒有任何東西被返回。我將不勝感激爲什麼這不起作用的任何幫助。謝謝。使用JQuery和Ajax獲取HTTP請求

文件名是 「trial.php」

<body> 
<script type="text/javascript" src="jquerycode.js"></script> 
<script type="text/javascript" src="ajax.js"></script> 
<input id = "string" type = "text" /> 
<input id = "button" type = "button" value = "Go" /> 
<div id = "feedback"></div> 
</body> 

文件名是 「ajax.js」

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

$.get('file.php', { input: string }, function(data) { 
$('#feedback').text(data); 
}); 
}); 

文件名是 「file.php」

<?php 
if(isset($_GET['input'])) 
{ 
$string = $_GET['input']; 
echo strtolower($string); 
} 
?> 
+0

也許,你可以打印在PHP端的數據,看看到底是怎麼回事。並且請避免調用您的變量字符串,這可能會導致很多瘋狂的錯誤。 – lc2817

+0

你使用螢火蟲檢查過它嗎?結果是什麼? –

+0

如果你執行'print_r($ string)',你會得到什麼,並檢查你通過'console.log(data)'或'alert(data)'獲得GET請求的成功回調的內容; – Rafay

回答

1

總結帶有document.ready事件的ajax.js:

$(document).ready(function() { 
    $('#button').click(function() { 
     var string = $('#string').val(); 

     $.get('file.php', { input: string }, function (data) { 
      $('#feedback').text(data); 
     }); 
    }); 
}); 
0
/*Try this*/ 
    $(document).ready(function(){ 
    $('#button').click(function() 
    { 

    var dataToSend={ input:$('#string').val() }; 

    $.ajax(url:'file.php', data:dataToSend,dataType:'html',type:'GET',success:function(data) { 
    $('#feedback').text(data); 
    }); 
    }); 
}); 
1
If you were not included any jquery file then please use any of jquery.min.js file at the top scipt function within <head> tage like: 

<script src="jquery-1.3.2.min.js" language="javascript" type="text/javascript"></script> 

And then modify your javascript function as given: 

$(document).ready(function(){ 
$('#button').click(function(){ 
    var string = $('#string').val(); 

    $.get('file.php', { input: string }, function(data) { 
    $('#feedback').text(data); 
    }); 
    }); 
});