标签:
{ "name":"Nicholas", "age":20 }
{ "name":"Nicholas", "age":19, "school":{ "name":"school", "location":"location" } }
[ { "title":"array", "author":"author" }, [ "title":"book", "author":{ "nameOne", "nameTwo" } ] ]
var book = { title:"title", author:[ "Nicholas C. Zakas" ], edition:3, year:2011 }; var jsonText = JSON.stringify(book); alert(jsonText); //{"title":"title","author":["Nicholas C. Zakas"],"edition":3,"year":2011}
var book = { title:"title", author:[ "Nicholas C. Zakas" ], edition:3, year:2011 }; var jsonText = JSON.stringify(book,["title","edition"]); //{"title":"title","edition":3}
如果是函数,则又有不同
var jsonText = JSON.stringify(book,function(key,value){ switch(key){ case "author": return value.join(","); case "year": return 5000; case "edition": return undefined; default: return value; } }); //{"title":"title","author":"Nicholas C. Zakas","year":5000}
var jsonText = JSON.stringify(book,null,4); //结果: { "title": "title", "author": [ "Nicholas C. Zakas" ], "edition": 3, "year": 2011 }
{ - -"title": "title", - -"author": [ - - - -"Nicholas C. Zakas" - -], - -"edition": 3, - -"year": 2011 }
var book = { title:"title", author:[ "Nicholas C. Zakas" ], edition:3, year:2011, releaseDate:new Date(2011,11,1) }; var jsonText = JSON.stringify(book); var bookCopy = JSON.parse(jsonText,function(key,value){ if(key == "releaseDate"){ return new Date(value); }else{ return value; } }); alert(bookCopy.releaseDate.getFullYear()); //2011
function createXHR(){ if(typeof XMLHttpRequest != "undefined"){ return new XMLHttpRequest; }else if(typeof ActiveXObject != "undefined"){ if(typeof arguments.callee.activeXString != "string"){ var versions = ["MSXML2.XMLHttp.6.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp"],i,len; for(i=0,len=versions.length;i < len;i++){ try{ new ActiveXObject(versions[i]); arguments.callee.activeXString = versions[i]; break; }catch(ex){ //跳过 } } } return new ActiveXObject(arguments.callee.activeXString); }else{ throw new Error("No XHR Object available"); } } //使用示例 var xhr = createXHR();
xhr.open("get","example.php",false);
xhr.send(null);
xhr.open("get","example.php",false); //同步请求 xhr.send(null); if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){ alert(xhr.resopnseText); }else{ alert("Request was unsuccessful: "+xhr.status); }
var xhr = createXHR(); xhr.onreadystatechange = function(){ //DOM0级方法,不是所有浏览器都支持DOM2级方法 if(xhr.readyState == 4){ if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){ alert(xhr.resopnseText); }else{ alert("Request was unsuccessful: "+xhr.status); } } }; xhr.open("get","example.php",true); //异步请求 xhr.send(null);
在接收到响应之前还可以调用abort方法来取消异步请求:
xhr.abort();
var xhr = createXHR(); xhr.onreadystatechange = function(){ if(xhr.readyState == 4){ if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){ alert(xhr.resopnseText); }else{ alert("Request was unsuccessful: "+xhr.status); } } }; xhr.open("get","example.php",true); xhr.setRequestHeader("MyHeader","MyValue"); xhr.send(null);
function submitData(){ var xhr = createXHR(); xhr.onreadystatechange = function(){ if(xhr.readyState == 4){ if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304){ alert(xhr.resopnseText); }else{ alert("Request was unsuccessful: "+xhr.status); } } }; xhr.open("post","postExample.php",true); xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); var form = document.getElementById("user-info"); xhr.send(serialize(form)); }
示例php文件:
<?php header("Content-Type:text/plain"); echo <<<EOF Name:{$_POST[‘user-name‘]} Email:{$_POST[‘user-email‘]} EOF; ?>
var data = new FormData(); data.append("name","Nicholas"); //接收两个参数:键,值
方法2、
var data = new FormData(document.forms[0]); //直接使用表单元素
方法3、
var form = document.getElementById("user-info"); xhr.send(new FormData(form));
var socket = new WebSocket("ws://www.example.com/server.php");
socket.close();
socket.send("hello world"); //任意字符串
var socket = new WebSocket("ws://www.example.com/server.php"); var CookieUtil = { get:function(name){ var cookieName = encodeURIComponent(name) + "=" , cookieStart = document.cookie.indexOf(cookieName), cookieValue = null; if(cookieStart > -1){ var cookieEnd = document.cookie.indexOf(";",cookieStart); if(cookieEnd == -1){ cookieEnd = document.cookie.length; } cookieValue = decodeURIComponent(document.cookie.substring(cookieStart+cookieName.length,cookieEnd)); } return cookieValue; }, set:function(name,value,expires,path,domain,secure){ var cookieText = encodeURIComponent(name) + "=" + encodeURIComponent(value); if(expires instanceof Date){ cookieText += "; expires=" + expires.toGMTString(); } if(path){ cookieText += "; path=" + path; } if(domain){ cookieText =+ "; domain=" + domain; } if(secure){ cookieText += "; secure=" + secure; } document.cookie = cookieText; }, unset:function(name,path,domain,secure){ this.set(name,"",new Date(0),path,domain,secure); } };
使用示例:
//设置cookie CookieUtil.set("name","Nicholas"); CookieUtil.set("book","Professional JavaScript"); //读取 alert(CookieUtil.get("name")); //"Nicholas" alert(CookieUtil.get("book")); //Professional JavaScript //删除 CookieUtil.unset("name"); CookieUtil.unset("book"); //设置cookie,包括它的路径、域、失效日期 CookieUtil.set("name","Nicholas","/books/projs","www.wrox.com",new Date("January 1,2010")); //删除刚设置的cookie CookieUtil.unset("name","/books/projs","www.wrox.com"); //设置安全cookie CookieUtil.set("name","Nicholas",null,null,null,true);
JavaScript高级程序设计(第三版)学习笔记20、21、23章
标签:
原文地址:http://www.cnblogs.com/TwinklingZ/p/5277146.html