jquery - getJSON() callback not working with PHP file -
php code=>`
<?php //request data database //code here connect database , data want /* example json format { "item1": "i love jquery4u", "item2": "you love jquery4u", "item3": "we love jquery4u" } */ //return in json format echo "{"; echo "item1: ", json_encode($item1), "\n"; echo "item2: ", json_encode($item2), "\n"; echo "item3: ", json_encode($item3), "\n"; echo "}"; ?>
js=>
$(document).ready(function(){ //attach jquery live event button $('#getdata-button').click(function(){ $.get('json-data.php', function(data) { //alert(data); //uncomment debug //alert (data.item1+" "+data.item2+" "+data.item3); //further debug $('#showdata').html("<p>item1="+data.item1+" item2="+data.item2+" item3="+data.item3+"</p>"); }); }); });
please i've asked earlier , wasnt including details time ive included whole sample when included apart console gives no error. thank `
you should add following header php file:
header('content-type: text/json');
besides that, put data directly array , use json_encode. php file should this:
<?php header('content-type: text/json'); $results = array( "item1"=> "i love jquery4u", "item2"=> "you love jquery4u", "item3"=> "we love jquery4u" ); echo json_encode($results); ?>
which give following result:
{ "item1": "i love jquery4u", "item2": "you love jquery4u", "item3": "we love jquery4u" }
Comments
Post a Comment