校友个 發表於 2019-10-4 12:01:00

angular 中的 http 请求

<p><span style="font-size: 18px"><strong>angular 中使用 http 请求的前提,需要引入 httpClientModule 模块</strong></span></p>
<p><span style="font-size: 18px"><strong>根模块中 app.module.ts:</strong></span></p>
<div style="color: rgba(197, 200, 198, 1); line-height: 19px; font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; font-weight: normal; white-space: pre; background-color: rgba(30, 30, 30, 1)">
<div><span style="color: rgba(152, 114, 162, 1)">import<span style="color: rgba(197, 200, 198, 1)">&nbsp;{&nbsp;<span style="color: rgba(96, 137, 180, 1)">HttpClientModule<span style="color: rgba(197, 200, 198, 1)">&nbsp;}&nbsp;<span style="color: rgba(152, 114, 162, 1)">from<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(154, 168, 58, 1)">'@angular/common/http'</span></span></span></span></span></span></span></div>
<div>&nbsp;</div>
</div>
<div style="color: rgba(197, 200, 198, 1); line-height: 19px; font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; font-weight: normal; white-space: pre; background-color: rgba(30, 30, 30, 1)">
<div><span style="color: rgba(197, 200, 198, 1)">imports:&nbsp;[</span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgba(96, 137, 180, 1)">BrowserModule<span style="color: rgba(197, 200, 198, 1)">,</span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgba(96, 137, 180, 1)">AppRoutingModule<span style="color: rgba(197, 200, 198, 1)">,</span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;<span style="text-decoration: underline"><strong><em>&nbsp;&nbsp;<span style="color: rgba(96, 137, 180, 1); background-color: rgba(0, 0, 0, 1)">HttpClientModule</span></em></strong></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;],</span></div>
</div>
<p><span style="font-size: 16px"><strong>组件中:</strong></span></p>
<div style="color: rgba(197, 200, 198, 1); line-height: 19px; font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; font-weight: normal; white-space: pre; background-color: rgba(30, 30, 30, 1)">
<div><span style="color: rgba(152, 114, 162, 1)">import<span style="color: rgba(197, 200, 198, 1)">&nbsp;{&nbsp;<span style="color: rgba(96, 137, 180, 1)">HttpClient<span style="color: rgba(197, 200, 198, 1)"><span style="color: rgba(96, 137, 180, 1)"><span style="color: rgba(197, 200, 198, 1)">}&nbsp;<span style="color: rgba(152, 114, 162, 1)">from<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(154, 168, 58, 1)">'@angular/common/http'</span></span></span></span></span></span></span></span></span></div>
</div>
<p>&nbsp;</p>
<div style="color: rgba(197, 200, 198, 1); line-height: 19px; font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; font-weight: normal; white-space: pre; background-color: rgba(30, 30, 30, 1)">
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(152, 114, 162, 1)">constructor<span style="color: rgba(197, 200, 198, 1)">(<span style="color: rgba(152, 114, 162, 1)">public<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(96, 137, 180, 1)">http<span style="color: rgba(103, 104, 103, 1)">:<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(155, 0, 0, 1)">HttpClient<span style="color: rgba(197, 200, 198, 1)"><span style="color: rgba(152, 114, 162, 1)"><span style="color: rgba(197, 200, 198, 1)"><span style="color: rgba(96, 137, 180, 1)"><span style="color: rgba(103, 104, 103, 1)"><span style="color: rgba(197, 200, 198, 1)"><span style="color: rgba(155, 0, 0, 1)"><span style="color: rgba(197, 200, 198, 1)">)&nbsp;{&nbsp;}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></div>
</div>
<p>&nbsp;</p>
<hr>
<p><span style="font-size: 16px"><strong>1. GET 请求写法:</strong></span></p>
<div style="color: rgba(197, 200, 198, 1); line-height: 19px; font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; font-weight: normal; white-space: pre; background-color: rgba(30, 30, 30, 1)">
<div><span style="color: rgba(206, 103, 0, 1)">getData<span style="color: rgba(197, 200, 198, 1)">()&nbsp;{</span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgba(152, 114, 162, 1)">let<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(96, 137, 180, 1)">url<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(103, 104, 103, 1)">=<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(154, 168, 58, 1)">'/search/interface/getrelatequery?word=%E6%99%8B%E6%B1%9F'</span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgba(199, 68, 74, 1)">this<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(96, 137, 180, 1)">http<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(206, 103, 0, 1)">get<span style="color: rgba(197, 200, 198, 1)">(<span style="color: rgba(96, 137, 180, 1)">url<span style="color: rgba(197, 200, 198, 1)">).<span style="color: rgba(206, 103, 0, 1)">subscribe<span style="color: rgba(197, 200, 198, 1)">((<span style="color: rgba(96, 137, 180, 1)">res<span style="color: rgba(103, 104, 103, 1)">:<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(199, 68, 74, 1)">any<span style="color: rgba(197, 200, 198, 1)">)<span style="color: rgba(152, 114, 162, 1)">=&gt;<span style="color: rgba(197, 200, 198, 1)">{</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgba(199, 68, 74, 1)">console<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(152, 114, 162, 1)">log<span style="color: rgba(197, 200, 198, 1)">(<span style="color: rgba(154, 168, 58, 1)">"GET&nbsp;请求"<span style="color: rgba(197, 200, 198, 1)">,&nbsp;<span style="color: rgba(96, 137, 180, 1)">res<span style="color: rgba(197, 200, 198, 1)">)</span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgba(199, 68, 74, 1)">this<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(152, 114, 162, 1)">newsList<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(103, 104, 103, 1)">=<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(96, 137, 180, 1)">res<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(96, 137, 180, 1)">data<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(152, 114, 162, 1)">relateQuery</span></span></span></span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;})</span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;}</span></div>
</div>
<p><span style="font-size: 16px"><strong>2. POST 请求写法:</strong></span></p>
<p><span style="font-size: 16px"><strong>post 请求必须设置请求头</strong></span></p>
<div style="color: rgba(197, 200, 198, 1); line-height: 19px; font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; font-weight: normal; white-space: pre; background-color: rgba(30, 30, 30, 1)">
<div><span style="color: rgba(152, 114, 162, 1)">import<span style="color: rgba(197, 200, 198, 1)">&nbsp;{&nbsp;<span style="color: rgba(96, 137, 180, 1)">HttpClient<span style="color: rgba(197, 200, 198, 1)">,&nbsp;<span style="color: rgba(96, 137, 180, 1)">HttpHeaders<span style="color: rgba(197, 200, 198, 1)">&nbsp;}&nbsp;<span style="color: rgba(152, 114, 162, 1)">from<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(154, 168, 58, 1)">'@angular/common/http'</span></span></span></span></span></span></span></span></span></div>
</div>
<p>&nbsp;</p>
<div style="color: rgba(197, 200, 198, 1); line-height: 19px; font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; font-weight: normal; white-space: pre; background-color: rgba(30, 30, 30, 1)">
<div><span style="color: rgba(206, 103, 0, 1)">postData<span style="color: rgba(197, 200, 198, 1)">()&nbsp;{</span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgba(152, 114, 162, 1)">let<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(96, 137, 180, 1)">api<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(103, 104, 103, 1)">=<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(154, 168, 58, 1)">'/api/message/readnotice'</span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgba(152, 114, 162, 1)">let<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(96, 137, 180, 1)">requestData<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(103, 104, 103, 1)">=<span style="color: rgba(197, 200, 198, 1)">&nbsp;{</span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;advert_id:&nbsp;<span style="color: rgba(154, 168, 58, 1)">'212'</span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;}</span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgba(152, 114, 162, 1)">let<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(96, 137, 180, 1)">headerOption<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(103, 104, 103, 1)">=<span style="color: rgba(197, 200, 198, 1)">&nbsp;{&nbsp;headers:&nbsp;<span style="color: rgba(103, 104, 103, 1)">new<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(155, 0, 0, 1)">HttpHeaders<span style="color: rgba(197, 200, 198, 1)">({&nbsp;<span style="color: rgba(154, 168, 58, 1)">"Content-Type"<span style="color: rgba(197, 200, 198, 1)">:&nbsp;<span style="color: rgba(154, 168, 58, 1)">'application/json'<span style="color: rgba(197, 200, 198, 1)">})}</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;</span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgba(199, 68, 74, 1)">this<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(96, 137, 180, 1)">http<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(206, 103, 0, 1)">post<span style="color: rgba(197, 200, 198, 1)">(<span style="color: rgba(96, 137, 180, 1)">api<span style="color: rgba(197, 200, 198, 1)">,&nbsp;<span style="color: rgba(96, 137, 180, 1)">requestData<span style="color: rgba(197, 200, 198, 1)">,&nbsp;<span style="color: rgba(96, 137, 180, 1)">headerOption<span style="color: rgba(197, 200, 198, 1)">).<span style="color: rgba(206, 103, 0, 1)">subscribe<span style="color: rgba(197, 200, 198, 1)">((<span style="color: rgba(96, 137, 180, 1)">res<span style="color: rgba(197, 200, 198, 1)">)<span style="color: rgba(152, 114, 162, 1)">=&gt;<span style="color: rgba(197, 200, 198, 1)">{</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgba(199, 68, 74, 1)">console<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(152, 114, 162, 1)">log<span style="color: rgba(197, 200, 198, 1)">(<span style="color: rgba(154, 168, 58, 1)">"POST请求:"<span style="color: rgba(197, 200, 198, 1)">,&nbsp;<span style="color: rgba(96, 137, 180, 1)">res<span style="color: rgba(197, 200, 198, 1)">)</span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;})</span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;}</span></div>
</div>
<p><span style="font-size: 16px"><strong>3. jsonp 请求:</strong></span></p>
<p><span style="font-size: 16px"><strong>jsonp 请求与前两种不同之处在与,除了引入 httpClientModule 之外,还要引入 HttpClientJsonpModule</strong></span></p>
<p><span style="font-size: 16px"><strong>根模块中:</strong></span></p>
<div style="color: rgba(197, 200, 198, 1); line-height: 19px; font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; font-weight: normal; white-space: pre; background-color: rgba(30, 30, 30, 1)">
<div><span style="color: rgba(152, 114, 162, 1)">import<span style="color: rgba(197, 200, 198, 1)">&nbsp;{&nbsp;<span style="color: rgba(96, 137, 180, 1)">HttpClientModule<span style="color: rgba(197, 200, 198, 1)">,&nbsp;<span style="color: rgba(96, 137, 180, 1)">HttpClientJsonpModule<span style="color: rgba(197, 200, 198, 1)">&nbsp;}&nbsp;<span style="color: rgba(152, 114, 162, 1)">from<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(154, 168, 58, 1)">'@angular/common/http'</span></span></span></span></span></span></span></span></span></div>
</div>
<p><span style="font-size: 16px"><strong>组件中:</strong></span></p>
<p><span style="font-size: 16px"><strong>如果不引入 HttpClientJsonModule , this.http.jsonp 报错</strong></span></p>
<div style="color: rgba(197, 200, 198, 1); line-height: 19px; font-family: Consolas, &quot;Courier New&quot;, monospace; font-size: 14px; font-weight: normal; white-space: pre; background-color: rgba(30, 30, 30, 1)">
<div><span style="color: rgba(154, 155, 153, 1)">/**</span></div>
<div><span style="color: rgba(154, 155, 153, 1)">&nbsp;&nbsp;&nbsp;*&nbsp;jsonp&nbsp;解决跨域</span></div>
<div><span style="color: rgba(154, 155, 153, 1)">&nbsp;&nbsp;&nbsp;*&nbsp;使用&nbsp;JSONP&nbsp;格式请求数据的前提是&nbsp;后台必须支持&nbsp;jsonp&nbsp;请求,&nbsp;请求的&nbsp;api&nbsp;中带有&nbsp;callBack&nbsp;或者&nbsp;cb</span></div>
<div><span style="color: rgba(154, 155, 153, 1)">&nbsp;&nbsp;&nbsp;*/</span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;<span style="color: rgba(206, 103, 0, 1)">getJsonpData<span style="color: rgba(197, 200, 198, 1)">()&nbsp;{</span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgba(152, 114, 162, 1)">let<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(96, 137, 180, 1)">url<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(103, 104, 103, 1)">=<span style="color: rgba(197, 200, 198, 1)">&nbsp;<span style="color: rgba(154, 168, 58, 1)">'/search/interface/getrelatequery?word=%E6%99%8B%E6%B1%9F'</span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgba(199, 68, 74, 1)">this<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(96, 137, 180, 1)">http<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(206, 103, 0, 1)">jsonp<span style="color: rgba(197, 200, 198, 1)">(<span style="color: rgba(96, 137, 180, 1)">url<span style="color: rgba(197, 200, 198, 1)">,&nbsp;<span style="color: rgba(154, 168, 58, 1)">'callback'<span style="color: rgba(197, 200, 198, 1)">).<span style="color: rgba(206, 103, 0, 1)">subscribe<span style="color: rgba(197, 200, 198, 1)">((<span style="color: rgba(96, 137, 180, 1)">res<span style="color: rgba(197, 200, 198, 1)">)<span style="color: rgba(152, 114, 162, 1)">=&gt;<span style="color: rgba(197, 200, 198, 1)">{</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: rgba(199, 68, 74, 1)">console<span style="color: rgba(197, 200, 198, 1)">.<span style="color: rgba(152, 114, 162, 1)">log<span style="color: rgba(197, 200, 198, 1)">(<span style="color: rgba(154, 168, 58, 1)">"JSOP&nbsp;请求数据"<span style="color: rgba(197, 200, 198, 1)">,&nbsp;<span style="color: rgba(96, 137, 180, 1)">res<span style="color: rgba(197, 200, 198, 1)">)</span></span></span></span></span></span></span></span></span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;&nbsp;&nbsp;})</span></div>
<div><span style="color: rgba(197, 200, 198, 1)">&nbsp;&nbsp;}</span></div>
</div>
<p>&nbsp;</p>
<hr>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><span style="font-size: 16px"><strong></strong></span></p><br><br>
来源:https://www.cnblogs.com/monkey-K/p/11621788.html
頁: [1]
查看完整版本: angular 中的 http 请求