ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# flyio/axios/qs fly文档中文<https://wendux.github.io/dist/#/doc/flyio/readme> <https://unpkg.com/flyio/dist/fly.min.js> axios中文文档<http://www.axios-js.com/> axios<https://cdn.bootcss.com/axios/0.19.0/axios.min.js> qs<https://cdn.bootcss.com/qs/6.7.0/qs.min.js>解决跨域 `npm install flyio` ``` <pre class="calibre14">``` fly<span class="token2">.</span><span class="token1">get</span><span class="token2">(</span><span class="token4">'{php echo $this->createWebUrl("ajax",["id"=>123])}'</span><span class="token2">)</span> <span class="token2">.</span><span class="token1">then</span><span class="token2">(</span><span class="token5">function</span> <span class="token2">(</span>response<span class="token2">)</span> <span class="token2">{</span> console<span class="token2">.</span><span class="token1">log</span><span class="token2">(</span>response<span class="token2">.</span>data<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token2">)</span> <span class="token2">.</span><span class="token5">catch</span><span class="token2">(</span><span class="token5">function</span> <span class="token2">(</span>error<span class="token2">)</span> <span class="token2">{</span> console<span class="token2">.</span><span class="token1">log</span><span class="token2">(</span>error<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token2">)</span><span class="token2">;</span> fly<span class="token2">.</span><span class="token1">get</span><span class="token2">(</span><span class="token4">'/user'</span><span class="token2">,</span> <span class="token2">{</span> id<span class="token2">:</span> <span class="token3">133</span> <span class="token2">}</span><span class="token2">)</span><span class="token2">.</span><span class="token1">then</span><span class="token2">(</span><span class="token2">(</span>response<span class="token2">)</span><span class="token">=</span><span class="token">></span><span class="token2">{</span> console<span class="token2">.</span><span class="token1">log</span><span class="token2">(</span>response<span class="token2">.</span>data<span class="token2">.</span>list<span class="token2">.</span>user<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token2">)</span> fly<span class="token2">.</span><span class="token1">post</span><span class="token2">(</span><span class="token4">'/user'</span><span class="token2">,</span> <span class="token2">{</span> name<span class="token2">:</span> <span class="token4">'Doris'</span><span class="token2">,</span> age<span class="token2">:</span> <span class="token3">24</span> phone<span class="token2">:</span><span class="token4">"18513222525"</span> <span class="token2">}</span><span class="token2">)</span> fly<span class="token2">.</span><span class="token1">request</span><span class="token2">(</span><span class="token4">'{php echo $this->createWebUrl("ajax",["id"=>123])}'</span><span class="token2">,</span><span class="token2">{</span><span class="token4">"qq"</span><span class="token2">:</span><span class="token4">"234"</span><span class="token2">}</span><span class="token2">,</span><span class="token2">{</span>method<span class="token2">:</span><span class="token4">"post"</span><span class="token2">}</span><span class="token2">)</span> php需要<span class="token1">file_get_contents</span><span class="token2">(</span><span class="token4">"php://input"</span><span class="token2">)</span><span class="token2">;</span>接收 var qs <span class="token">=</span> <span class="token1">require</span><span class="token2">(</span><span class="token4">'qs'</span><span class="token2">)</span><span class="token2">;</span> <span class="token6">//解决正常$_POST/$_GET接收</span> fly<span class="token2">.</span><span class="token1">post</span><span class="token2">(</span><span class="token4">'/foo'</span><span class="token2">,</span> qs<span class="token2">.</span><span class="token1">stringify</span><span class="token2">(</span><span class="token2">{</span> <span class="token4">'bar'</span><span class="token2">:</span> <span class="token3">123</span> <span class="token2">}</span><span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> ``` ``` `npm install axios` ``` <pre class="calibre16">``` axios<span class="token2">.</span><span class="token1">get</span><span class="token2">(</span><span class="token4">'/user?ID=12345'</span><span class="token2">)</span> <span class="token2">.</span><span class="token1">then</span><span class="token2">(</span><span class="token5">function</span> <span class="token2">(</span>response<span class="token2">)</span> <span class="token2">{</span> console<span class="token2">.</span><span class="token1">log</span><span class="token2">(</span>response<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token2">)</span> <span class="token2">.</span><span class="token5">catch</span><span class="token2">(</span><span class="token5">function</span> <span class="token2">(</span>error<span class="token2">)</span> <span class="token2">{</span> console<span class="token2">.</span><span class="token1">log</span><span class="token2">(</span>error<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span><span class="token2">)</span><span class="token2">;</span> axios<span class="token2">.</span><span class="token1">get</span><span class="token2">(</span><span class="token4">'/user'</span><span class="token2">,</span> <span class="token2">{</span> params<span class="token2">:</span> <span class="token2">{</span> ID<span class="token2">:</span> <span class="token3">12345</span> <span class="token2">}</span> <span class="token2">}</span><span class="token2">)</span> axios<span class="token2">.</span><span class="token1">post</span><span class="token2">(</span><span class="token4">'/user'</span><span class="token2">,</span> <span class="token2">{</span> firstName<span class="token2">:</span> <span class="token4">'Fred'</span><span class="token2">,</span> lastName<span class="token2">:</span> <span class="token4">'Flintstone'</span> <span class="token2">}</span><span class="token2">)</span> <span class="token1">axios</span><span class="token2">(</span><span class="token2">{</span> method<span class="token2">:</span> <span class="token4">'post'</span><span class="token2">,</span> url<span class="token2">:</span> <span class="token4">'/user/12345'</span><span class="token2">,</span> data<span class="token2">:</span> <span class="token2">{</span> firstName<span class="token2">:</span> <span class="token4">'Fred'</span><span class="token2">,</span> lastName<span class="token2">:</span> <span class="token4">'Flintstone'</span> <span class="token2">}</span> <span class="token2">}</span><span class="token2">)</span><span class="token2">;</span> const qs <span class="token">=</span> <span class="token1">require</span><span class="token2">(</span><span class="token4">'qs'</span><span class="token2">)</span><span class="token2">;</span> axios<span class="token2">.</span><span class="token1">post</span><span class="token2">(</span><span class="token4">'/foo'</span><span class="token2">,</span> qs<span class="token2">.</span><span class="token1">stringify</span><span class="token2">(</span><span class="token2">{</span> <span class="token4">'bar'</span><span class="token2">:</span> <span class="token3">123</span> <span class="token2">}</span><span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> ``` ```