{"id":387,"date":"2016-11-22T23:41:00","date_gmt":"2016-11-22T15:41:00","guid":{"rendered":"http:\/\/loveuhouse.cn\/wordpress\/?p=387"},"modified":"2017-04-19T14:19:55","modified_gmt":"2017-04-19T06:19:55","slug":"ajax-%e5%b0%81%e8%a3%85%e6%88%90-%e6%88%91%e5%96%9c%e6%ac%a2%e7%9a%84%e6%a0%b7%e5%ad%90","status":"publish","type":"post","link":"https:\/\/loveuhouse.cn\/wordpress\/?p=387","title":{"rendered":"ajax \u5c01\u88c5\u6210 \u6211\u559c\u6b22\u7684\u6837\u5b50"},"content":{"rendered":"<p>\u9700\u8981jquery\u652f\u6301<\/p>\n<pre class=\"lang:js decode:true \">\/\/\u521b\u5efaajax\u7c7b\nfunction ajaxobj(){\n  this.url=\"\";\n  this.data=\"\";\n  this.type=\"post\";\n  this.async=true;\/\/true\uff1a\u540c\u6b65     false\uff1a\u5f02\u6b65\n  this.success;\/\/\u6210\u529f\u65f6\u6267\u884c\n    this.error;\/\/\u5931\u8d25\u65f6\u6267\u884c\n  this.complete=function(XHR,TextStatus){\/\/\u5b8c\u6210\u65f6\u6267\u884c\n   if(XHR.statusText!=\"OK\"){\n            if(typeof this.error == \"function\"){this.error();}\n            console.warn(that.url+\"->\"+XHR.statusText)\n    }\n  };\n  this.timeout=120000;\/\/120\u79d2\u8d85\u65f6\n  this.send=function(){\n    $.ajax(this);\n  }\n}\nfunction ajax(url,_post,callback,error){\n      var _ajax = new ajaxobj();\n      _ajax.url=url;\n      _ajax.data=_post;\n      _ajax.success=callback;\n      _ajax.error=error;\n      _ajax.send();\n    }\n<\/pre>\n<p>&nbsp;<br \/>\n\u539f\u751fjs  ajax<br \/>\n\u6765\u6e90<a href=\"http:\/\/www.cnblogs.com\/a757956132\/p\/5603176.html\">http:\/\/www.cnblogs.com\/a757956132\/p\/5603176.html<\/a><\/p>\n<pre class=\"lang:js decode:true\">\n\/* \u5c01\u88c5_ajax\u51fd\u6570\n * @param {string}opt.type http\u8fde\u63a5\u7684\u65b9\u5f0f\uff0c\u5305\u62ecPOST\u548cGET\u4e24\u79cd\u65b9\u5f0f\n * @param {string}opt.url \u53d1\u9001\u8bf7\u6c42\u7684url\n * @param {boolean}opt.async \u662f\u5426\u4e3a\u5f02\u6b65\u8bf7\u6c42\uff0ctrue\u4e3a\u5f02\u6b65\u7684\uff0cfalse\u4e3a\u540c\u6b65\u7684\n * @param {object}opt.data \u53d1\u9001\u7684\u53c2\u6570\uff0c\u683c\u5f0f\u4e3a\u5bf9\u8c61\u7c7b\u578b\n * @param {function}opt.success ajax\u53d1\u9001\u5e76\u63a5\u6536\u6210\u529f\u8c03\u7528\u7684\u56de\u8c03\u51fd\u6570\n *\/\n   function _ajax(opt) {\n        opt = opt || {};\n        opt.method = opt.method.toUpperCase() || 'POST';\n        opt.url = opt.url || '';\n        opt.async = opt.async || true;\n        opt.data = opt.data || null;\n        opt.success = opt.success || function () {};\n        opt.error = opt.error|| function () {};\n        var xmlHttp = null;\n        if (XMLHttpRequest) {\n            xmlHttp = new XMLHttpRequest();\n        }\n        else {\n            xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');\n        }var params = [];\n        for (var key in opt.data){\n            params.push(key + '=' + opt.data[key]);\n        }\n        var postData = params.join('&');\n        if (opt.method.toUpperCase() === 'POST') {\n            xmlHttp.open(opt.method, opt.url, opt.async);\n            xmlHttp.setRequestHeader('Content-Type', 'application\/x-www-form-urlencoded;charset=utf-8');\n            xmlHttp.send(postData);\n        }\n        else if (opt.method.toUpperCase() === 'GET') {\n            xmlHttp.open(opt.method, opt.url + '?' + postData, opt.async);\n            xmlHttp.send(null);\n        }\n        xmlHttp.onreadystatechange = function () {\n            if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {\n              opt.success(xmlHttp.responseText);\n            }\n            if(xmlHttp.readyState == 4 && xmlHttp.status != 200){\n              opt.error(xmlHttp);\n              console.warn(`[${opt.method}]${opt.url} -> ${xmlHttp.status}-${xmlHttp.statusText}`)\n            }\n        };\n    }\n        \/**\n        * \u6211\u559c\u6b22\u7684\u6837\u5b50\n        *\/\n    function ajax(url,_post,callback,error){\n      _ajax({\n        method: 'POST',\n        url: url,\n        data: _post,\n        success: function (response) {\n           callback(response);\n        },\n        error: error\n    });\n    }\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u8981jquery\u652f\u6301 \/\/\u521b\u5efaajax\u7c7b function ajaxobj(){ this.url=&#8221;&#8221;; t &hellip; <a href=\"https:\/\/loveuhouse.cn\/wordpress\/?p=387\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">ajax \u5c01\u88c5\u6210 \u6211\u559c\u6b22\u7684\u6837\u5b50<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[],"_links":{"self":[{"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/387"}],"collection":[{"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=387"}],"version-history":[{"count":4,"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/387\/revisions"}],"predecessor-version":[{"id":616,"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/387\/revisions\/616"}],"wp:attachment":[{"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/loveuhouse.cn\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}