jquery - How to run ajax in wamp server -
i working on ajax days , still won't work. decided download sample program using ajax still won't work. there wrong server? doens't return error. not show jquery.
client.php
<html> <head> <script language="javascript" type="text/javascript" src="jquery.js"></script> </head> <body> <!--------------------------------------------------------------------------------------------- 1) create html content can accessed jquery ----------------------------------------------------------------------------------------------> <h2> client example </h2> <h3>output: </h3> <div id="output">this element accessed jquery , text replaced</div> <script id="source" language="javascript" type="text/javascript"> $(function () { //------------------------------------------------------------------------------------------- // 2) send http request ajax http://api.jquery.com/jquery.ajax/ //------------------------------------------------------------------------------------------- $.ajax({ url: 'api.php', //the script call data data: "", //you can insert url argumnets here pass api.php example "id=5&parent=6" datatype: 'json', //data format success: function(data) //on recieve of reply { var id = data[0]; //get id var vname = data[1]; //get name //-------------------------------------------------------------------------------------- // 3) update html content //-------------------------------------------------------------------------------------- $('#output').html("<b>id: </b>"+id+"<b> name: </b>"+vname); //set output element html //recommend reading on jquery selectors awesome http://api.jquery.com/category/selectors/ } }); }); </script> </body> </html>
api.php
<?php //------------------------------------------------------------------------ //-------------------------------------------------------------------------- $host = "localhost"; $user = "root"; $pass = "root"; $databasename = "ajax01"; $tablename = "variables"; //-------------------------------------------------------------------------- // 1) connect mysql database //-------------------------------------------------------------------------- $con = mysql_connect($host,$user,$pass); $dbs = mysql_select_db($databasename, $con); //-------------------------------------------------------------------------- // 2) query database data //-------------------------------------------------------------------------- $result = mysql_query("select * $tablename"); //query $array = mysql_fetch_row($result); //fetch result //-------------------------------------------------------------------------- // 3) echo result json //-------------------------------------------------------------------------- echo json_encode($array); ?>
i guess link fine put console.log(data)
, check coming using browser developer tools.
oh , in php use header in first line header('content-type: application/json');
Comments
Post a Comment