{"id":4887,"date":"2016-03-16T16:55:56","date_gmt":"2016-03-16T08:55:56","guid":{"rendered":"https:\/\/sdeno.com\/?p=4887"},"modified":"2017-09-07T11:48:14","modified_gmt":"2017-09-07T03:48:14","slug":"%e5%8e%9f%e7%94%9fajax%e7%ac%94%e8%ae%b0","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=4887","title":{"rendered":"\u539f\u751fajax\u7b14\u8bb0"},"content":{"rendered":"<p><a href=\"https:\/\/sdeno.com\/wp-content\/uploads\/2016\/03\/ajax.txt\">_ajax\u7b14\u8bb0.txt<\/a><\/p>\n<pre> \/\/\u4ee5\u4e0b\u662f\u540c\u6b65\r\n var xhr=new XMLHttpRequest(); \/\/\u7528ajax\u5c31\u8981\u521b\u5efa\u4e00\u4e2aXHR\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u4e0d\u517c\u5bb9IE6\r\n jQuery(document).click(function(){\r\n  xhr.open('get','1.php?'+Math.random(),false); \/\/get\u65b9\u5f0f\uff0c\u8bf7\u6c42\u5730\u57401.php,\u540c\u6b65\u3002\u8fd9\u6b65\u53ea\u662f\u8d1f\u8d23\u5de5\u4f5c\u524d\u7684\u51c6\u5907,\u6ca1\u771f\u6b63\u542f\u52a8ajax\u3002\r\n  xhr.send(null);\/\/\u5982\u679c\u6709\u6570\u636e\u987a\u4fbf\u53d1\u9001\u7ed9\u670d\u52a1\u5668\u5c31\u586b\u5199,\u5982\u679c\u6ca1\u6709\u4e5f\u5fc5\u987b\u8981\u5199null,\u771f\u6b63\u542f\u52a8ajax\r\n  if(xhr.status==200){ \/\/\u8981\u60f3\u83b7\u53d6\u670d\u52a1\u5668\u4f20\u8fc7\u6765\u7684\u6570\u636e\u5c31\u8981\u5224\u65ad\uff0c\u662f\u5426\u8bf7\u6c42\u6210\u529f\r\n    document.title=xhr.responseText;\/\/\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u5230\u7684\u6570\u636e\r\n  }else{\r\n    document.title='\u51fa\u9519';\r\n  }\r\n });<\/pre>\n<p>&nbsp;<\/p>\n<pre>\/\/\u4ee5\u4e0b\u662f\u5f02\u6b65\r\n var xhr=new XMLHttpRequest(); \/\/\u7528ajax\u5c31\u8981\u521b\u5efa\u4e00\u4e2aXHR\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u4e0d\u517c\u5bb9IE6\r\n jQuery(document).click(function(){\r\n  xhr.onreadystatechange=function(){\r\n    if(xhr.status==200 &amp;&amp; xhr.readyState==4){ \/\/\u8981\u60f3\u83b7\u53d6\u670d\u52a1\u5668\u4f20\u8fc7\u6765\u7684\u6570\u636e\u5c31\u8981\u5224\u65ad\uff0c\u662f\u5426\u8bf7\u6c42\u6210\u529f\r\n      document.title=xhr.responseText;\/\/\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u5230\u7684\u6570\u636e\r\n      \/\/ xhr.getAllResponseHeaders(); \/\/\u83b7\u53d6\u5230\u54cd\u5e94\u7684\u5168\u90e8\u4fe1\u606f\r\n      \/\/ xhr.getResponseHeader('Content-Type'); \/\/\u83b7\u53d6 Content-Type\u4fe1\u606f\r\n    }else{\r\n        document.title='\u51fa\u9519';\r\n    }\r\n  }\r\n  xhr.open('get','1.php?'+Math.random(),true);\r\n  xhr.send(null);\/\/\u5982\u679c\u6709\u6570\u636e\u987a\u4fbf\u53d1\u9001\u7ed9\u670d\u52a1\u5668\u5c31\u586b\u5199,\u5982\u679c\u6ca1\u6709\u4e5f\u5fc5\u987b\u8981\u5199null,\u771f\u6b63\u542f\u52a8ajax\r\n  \/\/xhr.abort() \/\/\u8fd9\u91cc\u53d6\u6d88\u5f02\u6b65\r\n });<\/pre>\n<p>&nbsp;<\/p>\n<pre>\/\/\u5f02\u6b65\uff0c\u7528post\u65b9\u5f0f\uff0c\u5411\u670d\u52a1\u5668\u4f20\u9012\u6570\u636e\r\n var xhr=new XMLHttpRequest(); \/\/\u7528ajax\u5c31\u8981\u521b\u5efa\u4e00\u4e2aXHR\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u4e0d\u517c\u5bb9IE6\r\n jQuery(document).click(function(){\r\n   xhr.onreadystatechange=function(){\r\n     if(xhr.status==200 &amp;&amp; xhr.readyState==4){ \/\/\u8981\u60f3\u83b7\u53d6\u670d\u52a1\u5668\u4f20\u8fc7\u6765\u7684\u6570\u636e\u5c31\u8981\u5224\u65ad\uff0c\u662f\u5426\u8bf7\u6c42\u6210\u529f\r\n          document.title=xhr.responseText;\/\/\u4ece\u670d\u52a1\u5668\u83b7\u53d6\u5230\u7684\u6570\u636e\r\n       \/\/ xhr.getAllResponseHeaders(); \/\/\u83b7\u53d6\u5230\u54cd\u5e94\u7684\u5168\u90e8\u4fe1\u606f\r\n       \/\/ xhr.getResponseHeader('Content-Type'); \/\/\u83b7\u53d6 Content-Type\u4fe1\u606f\r\n     }else{\r\n          document.title='\u51fa\u9519';\r\n     }\r\n   }\r\n   xhr.open('post','1.php?'+Math.random(),true);\r\n   xhr.setRequestHeader('Content-Type', 'application\/x-www-form-urlencoded'); \/\/\u6a21\u62df\u8868\u5355\u63d0\u4ea4\uff0c\u4e0d\u7136\u7528post\u63d0\u4ea4\u6570\u636e\u7ed9\u670d\u52a1\u5668\u662f\u63a5\u53d7\u4e0d\u5230\u6570\u636e\u7684\r\n   xhr.send('name=xgllseo&amp;age=100');\/\/\u8fd9\u4e9b\u5199\u4f60\u8981\u4f20\u9012\u7684\u6570\u636e,\u5982\u679c\u6ca1\u6709\u4e5f\u5fc5\u987b\u8981\u5199null,\u771f\u6b63\u542f\u52a8ajax\r\n });<\/pre>\n<p>&nbsp;<\/p>\n<pre> \/\/\u83b7\u53d6json\u6570\u636e\r\n var xhr=new XMLHttpRequest(); \/\/\u7528ajax\u5c31\u8981\u521b\u5efa\u4e00\u4e2aXHR\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u4e0d\u517c\u5bb9IE6\r\n jQuery(document).click(function(){\r\n     xhr.onreadystatechange=function(){\r\n       if(xhr.status==200 &amp;&amp; xhr.readyState==4){ \/\/\u8981\u60f3\u83b7\u53d6\u670d\u52a1\u5668\u4f20\u8fc7\u6765\u7684\u6570\u636e\u5c31\u8981\u5224\u65ad\uff0c\u662f\u5426\u8bf7\u6c42\u6210\u529f\r\n          document.title=JSON.parse(xhr.responseText);\/\/\u83b7\u53d6json\u6570\u636e\r\n       }else{\r\n          document.title='\u51fa\u9519';\r\n       }\r\n      }\r\n      xhr.open('get','1.json?'+Math.random(),true);\r\n      xhr.send('name=xgllseo&amp;age=100');\/\/\u8fd9\u4e9b\u5199\u4f60\u8981\u4f20\u9012\u7684\u6570\u636e,\u5982\u679c\u6ca1\u6709\u4e5f\u5fc5\u987b\u8981\u5199null,\u771f\u6b63\u542f\u52a8ajax\r\n });<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<pre>\/\/\u8de8\u57df \u5e76\u53d1\u9001\u6570\u636e\u7ed9\u670d\u52a1\u5668\r\nfunction ajax(params) { \r\n params = params || {}; \r\n params.data = params.data || {}; \r\n var json = params.jsonp ? jsonp(params) : json(params); \r\n \/\/ jsonp\u8bf7\u6c42 \r\n function jsonp(params) { \r\n \/\/\u521b\u5efascript\u6807\u7b7e\u5e76\u52a0\u5165\u5230\u9875\u9762\u4e2d \r\n var callbackName = params.jsonp; \r\n var head = document.getElementsByTagName('head')[0]; \r\n \/\/ \u8bbe\u7f6e\u4f20\u9012\u7ed9\u540e\u53f0\u7684\u56de\u8c03\u53c2\u6570\u540d \r\n params.data['callback'] = callbackName; \r\n var data = formatParams(params.data); \r\n var script = document.createElement('script'); \r\n head.appendChild(script); \r\n \/\/\u521b\u5efajsonp\u56de\u8c03\u51fd\u6570 \r\n window[callbackName] = function(json) { \r\n   head.removeChild(script); \r\n   clearTimeout(script.timer); \r\n   window[callbackName] = null; \r\n     params.success &amp;&amp; params.success(json); \r\n   }; \r\n\u3000\u3000\/\/\u53d1\u9001\u8bf7\u6c42 \r\n script.src = params.url + '?' + data; \r\n \/\/\u4e3a\u4e86\u5f97\u77e5\u6b64\u6b21\u8bf7\u6c42\u662f\u5426\u6210\u529f\uff0c\u8bbe\u7f6e\u8d85\u65f6\u5904\u7406 \r\n  if(params.time) { \r\n    script.timer = setTimeout(function() { \r\n      window[callbackName] = null; \r\n      head.removeChild(script); \r\n      params.error &amp;&amp; params.error({ \r\n        message: '\u8d85\u65f6' \r\n      }); \r\n    }, time); \r\n   } \r\n }; \r\n \/\/\u683c\u5f0f\u5316\u53c2\u6570 \r\n function formatParams(data) { \r\n var arr = []; \r\n for(var name in data) { \r\n arr.push(encodeURIComponent(name) + '=' + encodeURIComponent(data[name])); \r\n }; \r\n \/\/ \u6dfb\u52a0\u4e00\u4e2a\u968f\u673a\u6570\uff0c\u9632\u6b62\u7f13\u5b58 \r\n arr.push('v=' + random()); \r\n return arr.join('&amp;'); \r\n } \r\n \/\/ \u83b7\u53d6\u968f\u673a\u6570 \r\n function random() { \r\n   return Math.floor(Math.random() * 10000 + 500); \r\n }\r\n}\r\n\r\n\r\n\r\najax({ \r\n url: 'test', \/\/ \u8bf7\u6c42\u5730\u5740\r\n jsonp: 'jsonpCallback', \/\/ \u91c7\u7528jsonp\u8bf7\u6c42\uff0c\u4e14\u56de\u8c03\u51fd\u6570\u540d\u4e3a\"jsonpCallbak\"\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u4e3a\u5408\u6cd5\u7684\u5b57\u7b26\u4e32\r\n data: {'b': '\u5f02\u6b65\u8bf7\u6c42'}, \/\/ \u4f20\u8f93\u6570\u636e\r\n success:function(res){ \/\/ \u8bf7\u6c42\u6210\u529f\u7684\u56de\u8c03\u51fd\u6570\r\n   console.log(res); \r\n },\r\n error: function(error) {} \/\/ \u8bf7\u6c42\u5931\u8d25\u7684\u56de\u8c03\u51fd\u6570\r\n});\r\n\r\n\r\n\r\n\u670d\u52a1\u5668\u8bbe\u7f6e\uff1a\r\n$data='[{name:\"chenge\"},{age:24}]'; \/\/\u8fd9\u91cc\u5c31\u662f\u8981\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\u7684\u6570\u636e\uff0c\u5fc5\u987b\u8fd8\u662fjson\u683c\u5f0f\u7684\uff0c\u4e0d\u7136\u5c31\u51fa\u9519\u3002\u4e4b\u524d\u8bbe\u7f6e\u4e86\u4e5f\u5fc5\u987b\u8bbe\u7f6edataType:\"jsonp\"\r\n$str=$_GET['callback'].'('.$data.')';\r\necho $str;<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>_ajax\u7b14\u8bb0.txt \/\/\u4ee5\u4e0b\u662f\u540c\u6b65 var xhr=new XMLHttpRequest(); \/\/\u7528aj [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-4887","post","type-post","status-publish","format-standard","hentry","category-11"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/4887","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4887"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/4887\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4887"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4887"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4887"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}