程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP基礎知識 >> Jquery和PHP Ajax JSON 基礎教程

Jquery和PHP Ajax JSON 基礎教程

編輯:PHP基礎知識
 

無緩存,無錯版
$.ajax({
type: "GET",
url: "index.php",
cache: false,
data: "con=Add&act=_search&key=" + key+"&id="+id,
dataType:"json",
success: function(msg){
bindGroupList(msg);
}
});

//綁定
function bindGroupList(result)
{
var eles = document.forms['theForm'].elements;
eles['group_id'].length = 1;
for (i = 0; i < result.content.length; i++)
{
var opt = document.createElement('OPTION');
opt.value = result.content[i].id;
opt.text = result.content[i].name;
eles['group_id'].options.add(opt);
}
}

會緩存
$.getJSON("?con=Add&act=_search&key=" + key+"&id="+id, function(data){
//bindGroupList(data)
});

=====php make_json_result=======
/**
* 創建一個JSON格式的數據
*
* @access public
* @param string $content
* @param integer $error
* @param string $message
* @param array $append
* @return void
*/
function make_json_response($content = '', $error = "0", $message = '', $append = array()) {
$res = array(
'error' => $error,
'message' => $message,
'content' => $content
);
if (!empty($append)) {
foreach ($append AS $key => $val) {
$res[$key] = $val;
}
}
$val = json_encode($res);

exit($val);
}

/**
*
*
* @access public
* @param
* @return void
*/
function make_json_result($content, $message = '', $append = array()) {
make_json_response($content, 0, $message, $append);
}

 

return make_json_result($list);

 
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved