{"id":6358,"date":"2018-01-08T11:28:29","date_gmt":"2018-01-08T03:28:29","guid":{"rendered":"https:\/\/sdeno.com:80\/?p=6358"},"modified":"2018-05-23T18:38:38","modified_gmt":"2018-05-23T10:38:38","slug":"vue%e4%b8%ad%e4%bd%bf%e7%94%a8ajax-vue-resource-js","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=6358","title":{"rendered":"vue\u4e2d\u4f7f\u7528ajax\u2014\u2014vue-resource.js"},"content":{"rendered":"<p>vue\u53ef\u4ee5\u5728\u5168\u5c40\u73af\u5883\u4e2d\u4f7f\u7528\u548c\u5c40\u90e8\u73af\u5883\u4f7f\u7528<\/p>\n<pre>\/\/ \u57fa\u4e8e\u5168\u5c40Vue\u5bf9\u8c61\u4f7f\u7528http\r\nVue.http.get('\/someUrl', [options]).then(function(){ console.log('\u6210\u529f\u56de\u8c03') }, function(){ console.log('\u5931\u8d25\u56de\u8c03') });\r\nVue.http.post('\/someUrl', [body], [options]).then(successCallback, errorCallback);\r\n\u6216\u8005\r\nVue.http({\r\n  url: 'http:\/\/example.com\/book',\r\n  method: 'JSONP',\r\n  jsonp: 'cb'\r\n});\r\n\r\n\/\/ \u5728\u4e00\u4e2aVue\u5b9e\u4f8b\u5185\u4f7f\u7528$http\r\nthis.$http.get('\/someUrl', [options]).then(successCallback, errorCallback);\r\nthis.$http.post('\/someUrl', [body], [options]).then(successCallback, errorCallback);<\/pre>\n<p>&nbsp;<\/p>\n<table class=\"post-table\" border=\"1\">\n<thead>\n<tr>\n<th width=\"134\">\u53c2\u6570<\/th>\n<th width=\"212\">\u7c7b\u578b<\/th>\n<th width=\"628\">\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td width=\"134\">url<\/td>\n<td width=\"212\"><code>string<\/code><\/td>\n<td width=\"628\">\u8bf7\u6c42\u7684URL<\/td>\n<\/tr>\n<tr>\n<td width=\"134\">method<\/td>\n<td width=\"212\"><code>string<\/code><\/td>\n<td width=\"628\">\u8bf7\u6c42\u7684HTTP\u65b9\u6cd5\uff0c\u4f8b\u5982\uff1a&#8217;GET&#8217;, &#8216;POST&#8217;\u6216\u5176\u4ed6HTTP\u65b9\u6cd5<\/td>\n<\/tr>\n<tr>\n<td width=\"134\">body<\/td>\n<td width=\"212\"><code>Object<\/code>,\u00a0<code>FormData<\/code>\u00a0<code>string<\/code><\/td>\n<td width=\"628\">request body<\/td>\n<\/tr>\n<tr>\n<td width=\"134\">params<\/td>\n<td width=\"212\"><code>Object<\/code><\/td>\n<td width=\"628\">\u8bf7\u6c42\u7684URL\u53c2\u6570\u5bf9\u8c61<\/td>\n<\/tr>\n<tr>\n<td width=\"134\">headers<\/td>\n<td width=\"212\"><code>Object<\/code><\/td>\n<td width=\"628\">request header\u00a0 \uff0c\u6d89\u53ca\u8de8\u57df\u65f6 \u65e0\u6548\uff0c<\/p>\n<p>\u5173\u4e8e\u8bf7\u6c42\u5934\u90e8\u4fe1\u606f\u53c2\u8003\uff1a<a href=\"https:\/\/sdeno.com\/?p=6375\" target=\"_blank\" rel=\"noopener\">https:\/\/sdeno.com\/?p=6375<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"134\">timeout<\/td>\n<td width=\"212\"><code>number<\/code><\/td>\n<td width=\"628\">\u5355\u4f4d\u4e3a\u6beb\u79d2\u7684\u8bf7\u6c42\u8d85\u65f6\u65f6\u95f4 (<code>0<\/code>\u00a0\u8868\u793a\u65e0\u8d85\u65f6\u65f6\u95f4)<\/td>\n<\/tr>\n<tr>\n<td width=\"134\">before<\/td>\n<td width=\"212\"><code>function(request)<\/code><\/td>\n<td width=\"628\">\u8bf7\u6c42\u53d1\u9001\u524d\u7684\u5904\u7406\u51fd\u6570\uff0c\u7c7b\u4f3c\u4e8ejQuery\u7684beforeSend\u51fd\u6570<\/td>\n<\/tr>\n<tr>\n<td width=\"134\">progress<\/td>\n<td width=\"212\"><code>function(event)<\/code><\/td>\n<td width=\"628\"><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/ProgressEvent\">ProgressEvent<\/a>\u56de\u8c03\u5904\u7406\u51fd\u6570<\/td>\n<\/tr>\n<tr>\n<td width=\"134\">credentials<\/td>\n<td width=\"212\"><code>boolean<\/code><\/td>\n<td width=\"628\">\u8868\u793a\u8de8\u57df\u8bf7\u6c42\u65f6\u662f\u5426\u9700\u8981\u4f7f\u7528\u51ed\u8bc1<\/td>\n<\/tr>\n<tr>\n<td width=\"134\">emulateHTTP<\/td>\n<td width=\"212\"><code>boolean<\/code><\/td>\n<td width=\"628\">\u53d1\u9001PUT, PATCH, DELETE\u8bf7\u6c42\u65f6\u4ee5HTTP POST\u7684\u65b9\u5f0f\u53d1\u9001\uff0c\u5e76\u8bbe\u7f6e\u8bf7\u6c42\u5934\u7684<code>X-HTTP-Method-Override<\/code><\/td>\n<\/tr>\n<tr>\n<td width=\"134\">emulateJSON<\/td>\n<td width=\"212\"><code>boolean<\/code><\/td>\n<td width=\"628\">\u5c06request body\u4ee5<code>application\/x-www-form-urlencoded<\/code>\u00a0content type\u53d1\u9001<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<div>\n<p>emulateHTTP<br \/>\nemulateHTTP\uff08\u5e03\u5c14\u503c\uff09\u3002\u9ed8\u8ba4\u503c\u4e3a:false\uff0c\u8bbe\u7f6e\u503c\u4e3atrue\u65f6\uff0cPUT, PATCH\u548cDELETE\u7b49\u8bf7\u6c42\u4f1a\u4ee5\u666e\u901a\u7684POST\u65b9\u6cd5\u53d1\u51fa\uff0c\u5e76\u4e14HTTP\u5934\u4fe1\u606f\u7684X-HTTP-Method-Override\u5c5e\u6027\u4f1a\u8bbe\u7f6e\u4e3a\u5b9e\u9645\u7684HTTP\u65b9\u6cd5\u3002\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>emulateJSON<br \/>\nemulateJSON(\u5e03\u5c14\u503c)\u3002\u9ed8\u8ba4\u503c\u4e3a\uff1afalse,\u8bbe\u7f6e\u503c\u4e3atrue\u65f6\uff0c\u6570\u636e\uff08\u65e0\u8bba\u662fget\u8fd8\u662fpost\uff09\u90fd\u4f1a\u4ee5formdata\uff08\u8868\u5355\u63d0\u4ea4\u65b9\u5f0f\uff09\u7684\u5f62\u5f0f\u53d1\u9001\u6570\u636e\u3002\u6240\u4ee5\u6211\u4eec\u5728\u7ed9\u540e\u53f0\u4f20\u9012\u53c2\u6570\u7684\u65f6\u5019\u9700\u8981\u52a0JSON.stringify(\u53c2\u6570)\u5904\u7406\u4e00\u4e0b\u3002<br \/>\n\u5982\u679c\u670d\u52a1\u5668\u65e0\u6cd5\u5904\u7406\u7f16\u7801\u4e3aapplication\/json\u7684\u8bf7\u6c42\uff0c\u53ef\u4ee5\u542f\u7528emulateJSON\u9009\u9879\u3002\u542f\u7528\u4e4b\u540e\uff0c\u8bf7\u6c42\u4f1a\u4ee5application\/x-www-form-urlencoded\u4e3aMIME type\uff0c\u5c31\u50cf\u666e\u901a\u7684HTML\u8868\u5355\u4e00\u6837\u3002<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">\u5168\u5c40\u8bbe\u7f6e\uff1a<\/span><\/p>\n<pre>Vue.http.options.emulateHTTP = true;\r\nVue.http.options.emulateJSON = true;<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">get:<\/span><\/p>\n<pre>this.$http.get('get.php',{params:{a:1,b:2}}).then(function(res){\r\n    alert(res.body); \r\n },function(res){\r\n    console.log(res.status);\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">post:<\/span><\/p>\n<p>\u5ba2\u6237\u7aef\u8981\u53d1\u6570\u636e\u7ed9\u540e\u53f0\u65f6<\/p>\n<pre>this.$http.post('post.php',{a:1,b:2}},{emulateJSON:true}).then(function(res){\r\n    alert(res.body); \r\n },function(res){\r\n    console.log(res.status);\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">jsonp:<\/span><\/p>\n<pre>url \uff1ahttps:\/\/sug.so.360.cn\/suggest?callback=suggest_so&amp;word=a\r\n\r\nnew Vue({\r\n  el:'#app',\r\n  mounted:function () {\r\n     this.$http.jsonp('https:\/\/sug.so.360.cn\/suggest',{\r\n        params:{word:'a'},  \/\/\u4f20\u7ed9\u670d\u52a1\u5668\u7684\u53c2\u6570\r\n        jsonp:'callback',   \/\/\u670d\u52a1\u5668\u90a3\u8fb9\u63a5\u6536\u7684\u5b57\u6bb5 $_POST['callback']\r\n        headers: {\"xxx\": \"xxxx\"}  \/\/\u5ba2\u6237\u7aef\u81ea\u5b9a\u4e49\u8bf7\u6c42\u5934\u4fe1\u606f\u4f20\u7ed9\u670d\u52a1\u5668\uff0c\u4f46\u662f\u5728\u8de8\u57df\u60c5\u51b5\u4e0b\u65e0\u6548\u3002\r\n     }).then(function(res){\r\n        console.log(res.data);\r\n     },function(res){\r\n        console.log('\u5931\u8d25');\r\n     });\r\n  }\r\n});\r\n\r\n\r\n\/\/\u53e6\u4e00\u79cd\u683c\u5f0f\r\n\r\nthis.$http({\r\n   url: 'https:\/\/sug.so.360.cn\/suggest',\r\n   params: {\r\n      word:'a'\r\n   },\r\n   method: 'JSONP',\r\n   jsonp: 'callback'  \/\/\u4e00\u822c\u9ed8\u8ba4\u90fd\u662fcallback\uff0c\u5982\u679c\u6709\u53d8\u5316\u6839\u636e\u63a5\u53e3\u7684\u89c4\u5219\u6765\uff0c\u4f8b\u5982\/suggest?callback187=suggest_so\uff0c\u6539\u6210jsonp: 'callback187'\r\n}).then(function (res) {\r\n    console.log(res.body);\r\n}, function (res) {\r\n    console.log('\u9519\u8bef\u56de\u8c03');\r\n});\r\n\r\n<\/pre>\n<p>\u8de8\u57df\u76f8\u5173\u5185\u5bb9\u67e5\u770b\uff1a<a href=\"https:\/\/sdeno.com\/?p=3647\" target=\"_blank\" rel=\"noopener\">https:\/\/sdeno.com\/?p=3647<\/a><\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #ff0000;\">interceptors\u6570\u636e\u52ab\u6301<\/span>\u00a0 \u7c7b\u4f3cbefore \u3001after<\/p>\n<pre>Vue.http.interceptors.push(function(request, next) {\r\n     \/\/ ...\r\n     \/\/ \u8bf7\u6c42\u53d1\u9001\u524d\u7684\u5904\u7406\u903b\u8f91 (\u7c7b\u4f3c$.ajax\u7684beforeSend,\u5728\u8bf7\u6c42\u670d\u52a1\u5668\u7684url\u4e4b\u524d\uff0c\u505a\u4e00\u4e9b\u5904\u7406\u4e4b\u540e\u5728\u8bf7\u6c42)\r\n     \/\/ ...\r\n     request.params.c='sdf'; \/\/\u4f8b\u5982\uff1a\u5728\u8bf7\u6c42\u670d\u52a1\u5668url\u524d\uff0c\u7a81\u7136\u53c8\u60f3\u989d\u5916\u52a0\u4e00\u4e2a\u53c2\u6570\r\n     request.headers.set('Server', 'asfsdf222');  \/\/\u8fd9\u91cc\u8bbe\u7f6e\u8bf7\u6c42\u5934\uff0c\u4e5f\u5c31\u662f\u5ba2\u6237\u7aef\u53d1\u7ed9\u670d\u52a1\u5668\u7684Request headers,\u5728\u670d\u52a1\u5668\u90a3\u91cc\u63a5\u6536\r\n  next(function(response) {\r\n     \/\/ ...\r\n     \/\/ \u8bf7\u6c42\u53d1\u9001\u540e\u7684\u5904\u7406\u903b\u8f91 \uff08\u5f53\u5ba2\u6237\u7aef\u8bf7\u6c42\u540e\u53f0\u6570\u636e\u65f6\uff0c\u540e\u53f0\u5373\u5c06\u8981\u53d1\u6570\u636e\u7ed9\u5ba2\u6237\u7aef\u4e4b\u524d\uff0c\u8fd9\u91cc\u53c8\u53ef\u4ee5\u989d\u5916\u7684\u505a\u4e00\u4e9b\u5904\u7406\uff09\r\n     \/\/ ...\r\n     \/\/ \u6839\u636e\u8bf7\u6c42\u7684\u72b6\u6001\uff0cresponse\u53c2\u6570\u4f1a\u8fd4\u56de\u7ed9successCallback\u6216errorCallback\r\n     response.body=response.body+'1231323';\r\n     return response\r\n  })\r\n});\r\n\r\n\r\n\r\nnew Vue({\r\n   el:'#app',\r\n   mounted:function () {\r\n       this.$http.get('http:\/\/www.vc.com\/v3\/blank',{params:{a:1,b:2}}).then(function(res){  \/\/\u8bf7\u6c42url\u524d\u53c8\u989d\u5916\u6dfb\u52a0\u4e86\u4e00\u4e2a\u53c2\u6570c=sdf\r\n           \/\/ alert(res.body);\r\n           console.log(res.body); \/\/&lt;---\u8fd9\u91cc\u8fd4\u56de\u7684\u6570\u636e\u5185\u5bb9\u591a\u4e86'1231323' \r\n       },function(res){\r\n           console.log(res.status);\r\n       });\r\n   }\r\n});<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/www.jb51.net\/article\/103159.htm\" target=\"_blank\" rel=\"noopener\">http:\/\/www.jb51.net\/article\/103159.htm<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>vue\u53ef\u4ee5\u5728\u5168\u5c40\u73af\u5883\u4e2d\u4f7f\u7528\u548c\u5c40\u90e8\u73af\u5883\u4f7f\u7528 \/\/ \u57fa\u4e8e\u5168\u5c40Vue\u5bf9\u8c61\u4f7f\u7528http Vue.http.get(&#8216;\/ [&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-6358","post","type-post","status-publish","format-standard","hentry","category-11"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/6358","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=6358"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/6358\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}