|
 |
|
ใน ajax ผมอยากแยกค่าที่เกิดจาก respons มานะครับ ว่าแยกได้เหรือป่าวครับ |
|
 |
|
|
 |
 |
|
ให้ php ส่งค่ากลับมาเป็น json format อย่างข้างล่างนี้ครับ
<?php
if (isset($_GET['ajax'])){
header('Content-type:application/json');
echo json_encode(array('a'=>'123','b'=>'456'));
die;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="th" xml:lang="th">
<head>
<meta http-equiv="content-type" content="text/html; charset=tis-620" />
<title>JSON Encoding</title>
<script type="text/javascript">
//<![CDATA[
var gObj;
function ajaxFunction(){
var xmlHttp;
try{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}catch (e){
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState==4){
if (xmlHttp.status==200){
//json object
eval('gObj='+xmlHttp.responseText);
alert('a='+gObj['a']+', b='+gObj['b']);
}else{
alert('error');
}
}
}
xmlHttp.open("GET","?ajax&"+Math.random(1000),true);
//กำหนด header
xmlHttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xmlHttp.send(null);
}
window.onload = function(){
ajaxFunction();
}
//]]>
</script>
</head>
<body>
</body>
</html>
|
 |
 |
 |
 |
Date :
2009-05-19 21:04:42 |
By :
num |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 00
|