jq之ajax以及json数据传递

<html>
 <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />     
<title>闭包演示</title>
<script src="js/jquery-2.1.1.min.js"></script>    
<script type="text/javascript">   

    function transfer(){
var dat = $( "#sel" ).val();
//console.log(dat)
   $.ajax({

   type:"POST",                                       //post 同 get

  url:"ts1.php",

  data:{data:dat},               
//console.log(data);
  success:function(data){
    var data = eval('(' + data + ')');
    console.log(data);
    console.log(data.authors[0]);return;
    //alert(data);
   $( '#sub' ).val( data );

  }

})

}
 </script>
 </head>
 
 <body>
  <select id = "sel" onchange = "transfer()">
   <option value = "welcome">欢迎</option>
   <option value = "hello">你好</option>
   <option value = "pity">可惜</option>
   <option value = "ok">好的</option>
  </select>
  <input type = "text" id = "sub" value = "name" />
 </body>
</html>

<?php
//$data[] = 'hello';
//
//$data = $_POST['data'];
//echo $data;
$array = array('a'=>'a1','b'=>'b1');

//print_r($array['a']) ;
$data='{
"title": "narto",
"authors": [
"Nicholas C. Zakas","a2"
],
"edition": 3,
"year": 2011
}';
$data=json_decode($data, true);
$data=json_encode($data);
print_r($data);

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
$json=json_decode($json, true);
//print_r($json);

// 演示可能出现的数据库部分 注:未连接数据库, 该部分只是演示说明
// $sql = 'SELECT * FROM `TABLE` WHERE ID = '.$data.';';
// $query = mysql_query( $sql );
// $result = mysql_fetch_row( $query );
//输出响应

?>

打印的

jq之ajax以及json数据传递

上一篇:AJ学IOS(36)UI之手势事件旋转_缩放_拖拽


下一篇:杂题