第十四篇、Ajax与Json

1.Ajax的核心知识

1.1 XMLHttpRequest对象

  function loadName(){

    var xmlHttp;

    if(window.XMLHttpRequest){

      xmlHttp = new XMLHttpRequest();

    }else{

      xmlHttp = new ActiveObject("Microsoft.XMLHTTP"); // IE 5  和 IE 6

    }

  }

1.2 XMLHttpRequest对象请求后台

  open(method,url,async)

  send()

    method: get或者post

    async(是否异步):true 或者 false

    send(string(参数)) 使用post方式

1.2 XMLHttpRequest对象响应服务器

1.3jQuery中的Ajax
jQuery作为一个使用人数最多的库,其Ajax很好的封装了原生Ajax的代码,在兼容性和易用性方面都做了很大的提高,让Ajax的调用变得非常简单。下面便是一段简单的jQuery的Ajax代码:
$.ajax(
  {
     method: 'GET', // 1.9.0本版前用'type' 
     url: "/test/", 
    dataType: 'json' }) 
.done(function()
   { console.log('执行成功'); }) 
.fail(function() { console.log('执行出错'); 
  }
)
与原生Ajax不同的是,jQuery中默认的Content-type是'application/x-www-form-urlencoded; charset=UTF-8', 想了解更多的jQuery Ajax的信息可以移步官方文档:http://api.jquery.com/jquery....
原文地址:https://www.cnblogs.com/HJQ2016/p/5860979.html