ajax
语法
//post请求 //创建ajax对象 var xhr = new XMLHttpRequest() //设置参数 xhr.open( /*方式(post),地址,是否异步的布尔值(默认异步,如果想要同步加false)*/ ) //发送请求 //post请求传递数据放在send方法中 //post请求传递数据要在send方法之前添加代码 xhr.setRequestHeader('content-type', 'applicati/x-www-form-urlencoded') xhr.send( /*'键=值&键=值'*/ ) //监听状态 xhr.onreadystatechange = function () { //对状态进行判断-数据是否传递完成 if (xhr.readyState === 4) { if (xhr.status >= 200 && xhr.status < 300) { //接收数据 xhr.responseText } } }
//get请求 //创建ajax对象 var xhr = new XMLHttpRequest() //设置参数 //get请求传递数据写在地址后:地址?键=值&键=值 xhr.open( /*方式(post),地址,是否异步的布尔值(默认异步,如果想要同步加false)*/ ) //发送请求 xhr.send() //监听状态 xhr.onreadystatechange = function () { //对状态进行判断-数据是否传递完成 if (xhr.readyState === 4) { if (xhr.status >= 200 && xhr.status < 300) { //接收数据 xhr.responseText } } }
json
为了方便操作,可以将json字符串转成对象或数组:
JSON.parse(json字符串)
网络传输数据的时候,只能以字符串的形式进行传递,可以将数组或对象转成json字符串:
JSON.stringify(数组或对象)

浙公网安备 33010602011771号