stringify()用法

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml">
 3 <head>
 4     <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
 5     <title></title>
 6     <script type="text/javascript" src="createXHR.js"></script>
 7     <script type="text/javascript" src="test02.js"></script>
 8 </head>
 9 <body>
10     
11 </body>
12 </html>

 

 1 function createXHR() {
 2     if (typeof XMLHttpRequest != "undefined") {
 3         return new XMLHttpRequest();
 4     } else if (typeof ActiveXObject != "undefined") {
 5         if (typeof arguments.callee.activeXString != "string") {
 6             var versions = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp"];
 7             for (var i = 0, len = versions.length; i < len; i++) {
 8                 try {
 9                     var xhr = new ActiveXObject(versions[i]);
10                     arguments.callee.activeXString = versions[i];
11                     return xhr;
12                 } catch (ex) {
13                     console.log("异常");
14                 }
15             }
16         }
17         return new ActiveXObject(arguments.callee.activeXString);
18     } else {
19         throw new Error("No XHR object available.");
20     }
21 }

 

 1 var xhr = createXHR();
 2 var contact = {
 3     name: "Ted Jones",
 4     email: "tedjones@some-other-domain.com"
 5 };
 6 xhr.onreadystatechange = function() {
 7     if (xhr.readyState == 4) {
 8         if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
 9             console.log(xhr.responseText);
10         }
11     }
12 };
13 xhr.open("post", "addcontact.php", true);
14 xhr.send(JSON.stringify(contact));

 

向服务器提交contact内容

posted @ 2012-06-08 11:19  小猩猩君  阅读(771)  评论(0编辑  收藏  举报