日期:2014-05-16  浏览次数:20598 次

jQuery学习第九课(ajax实例和插件编写模板及实例)

一、ajax实例

1.前端与程序员之间的数据约定

json(常用),text文本,xml

2.处理不同种类数据的方法

<script>
  $('input').click(function(){
   $.ajax({
    //
url:'test.txt?'+Math.random(),

    url:'demo1.php?'+Math.random(),
    type:'get',
    success:function(data){
     $('div').html(data);
    }
   });
  });
 </script>

<script>
  $('input').click(function(){
   var ul = $('#info>ul');
   $.ajax({
    
url:'demo2.php?'+Math.random(),
    type:'get',
    dataType:'xml',
    success:function(xml){
     $(xml).find('title').each(function(){
      //操作xml文件时,html()方法不可用
      //alert($(this).text())
      ul.append('<li>'+$(this).text()+'</li>');
     });
    }
   });
  });
 </script>

3.ajax执行时的提示

4.ajax中的细节(编码和提交方式

<!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" xml:lang="en">
<head>
 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
 <title>demo9</title>
 <script src="jquery.js"></script>
</head>
<body>
 <input type="button" value="获取数据" />
 <div></div>
 <span style="color:red"></span>
 <script>
  $('input').click(function(){
   $.ajax({
    
url:'demo9.php?'+Math.random(),
    type:'get',
    timeout:1000,
    success:function(data){
     $('div').html(data);
    },
    error:function(jqXHR,textStatus,errrorThrown){
     if(errrorThrown=='Not Found'){
      $('span').html('你请求的地址不存在');
     }
     if(textSta