SOLVED
problem solved, please see bottom answer for my solution
i've been struggling with this, i read tutorials about $.ajax()
here's the script
<script src="jquery-1.7.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(
function()
{
$('#getData').click(
function()
{
$.ajaxSetup({
cache: false
});
$.ajax(
{
type: 'GET',
url: 'http://localhost/Practice/Jquery/ajax/phone_data.php',
data: {rating: 5},
dataType: 'json',
success: function(data)
{
$('#display').html(data.phone);
},
error: function(response)
{
alert(response.responseText);
}
});//end ajax
});//end click
});//end ready
</script>
<body>
<input type="button" id="getData" name="submitButton" value="getJson!" />
<div id="display">
replace me with phone data
</div>
</body>
and this is the phone_data.php
<?php
header('Content-Type: application/json; charset=utf-8');
$data['phone'] = '989898989';
$data['username'] = 'sendyHalim';
echo json_encode($data, true);
exit();
when i click the button, firebug displays the request is OK, but nothing happens..
so i check the 'response' for the ajax call through firebug and get :
Reload the page to get source for: http://localhost/Practice/Jquery/ajax/phone_data.php?rating=5&_=1368793325734
Update
i updated my code , i search some way around to get the response text(using responseText
property from response
), but it just alert with empty string(nothing).
here's the output when i run phone_data.php script alone:
{"phone":"989898989","username":"sendyHalim"}
and "view page info"
from firefox confirms that the content-type is "application/json"
error
callback function on your AJAX call and see what error jQuery is reporting. – Anthony Grist May 17 '13 at 12:27$.get
and when i display the json response, it displays undefined(with the same php script) – mohur May 17 '13 at 12:36header('Content-type: application/json');
and addexit()
; at the end afterecho json_encode..
and try – bipen May 17 '13 at 12:36