ajax:php
mui.ajax('http://server-name/login.php',{ data:{ username:'username', password:'password' }, dataType:'json',//服務器返回json格式數據 type:'post',//HTTP請求類型 timeout:10000,//超時時間設置爲10秒; success:function(data){ //服務器返回響應,根據響應結果,分析是否登陸成功; ... }, error:function(xhr,type,errorThrown){ //異常處理; console.log(type); } });
後臺php:html
<?php header('Content-type:text/html; Charset=utf8'); header( "Access-Control-Allow-Origin:*"); header('Access-Control-Allow-Methods:POST'); header('Access-Control-Allow-Headers:x-requested-with,content-type'); include('../../conn.php'); include('../../luanma.php'); $sql="select * from news where class='校園新聞' order by time ASC"; mysql_query("SET NAMES utf8"); $result= mysql_query($sql); while($array=mysql_fetch_array($result ,MYSQL_ASSOC)){ $tt[]=array('tittle'=>$array["tittle"],'txt'=>$array["text"],'source'=>$array['source'],'date'=>$array['date']); } echo json_encode($tt); mysql_close(); ?>
必不可少的:mysql
header('Content-type:text/html; Charset=utf8'); header( "Access-Control-Allow-Origin:*"); header('Access-Control-Allow-Methods:POST'); header('Access-Control-Allow-Headers:x-requested-with,content-type');
轉自kevin_cyj,略作了寫修改,原博客地址 http://blog.csdn.net/kevin_cyj/article/details/51090668