助教天團
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Help
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
1
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# 🏅Day17 - Axios 基本介紹使用 # Axios 在開始實作註冊功能之前,需要先了解 `Axios` 這個第三方套件: `Axios` 是基於 `Promise` 的 HTTP 請求庫的設計,使得開發者可以輕鬆地與後端 `API` 進行串接, 比起 `AJAX` 更輕量化,且寫起來更加的直接、閱讀性更好。 它提供了簡單的 `API` 來執行 `GET`、`POST`、`PUT`、`DELETE` 等各種 `HTTP` 請求, 另外既然是使用 `Promise`,當然就可以使用像是 `.then`、`.catch` 等非同步操作語法, 也可以在發送 `request` 之前、或是獲得 `response` 之後,進行轉換資料的操作。 ## Axios 基本使用 `Axios` 載入之後,可以直接使用像是 `axios.get` 的方式直接去獲取遠端資料, 從範例中可以看到利用 `Axios` 很簡單的撈取遠端的資料: <iframe height="300" style="width: 100%;" scrolling="no" title="axios.get 範例" src="https://codepen.io/yen-kg/embed/vYwooPO?default-tab=html%2Cresult" frameborder="no" loading="lazy" allowtransparency="true" allowfullscreen="true"> See the Pen <a href="https://codepen.io/yen-kg/pen/vYwooPO"> axios.get 範例</a> by Yen Kg (<a href="https://codepen.io/yen-kg">@yen-kg</a>) on <a href="https://codepen.io">CodePen</a>. </iframe> --- 而 Axios 常見的請求方式總共有五種: * GET:從伺服器獲取數據。 ``` axios.get(url, config); ``` * POST:向伺服器提交數據。 ``` axios.post(url, data, config); ``` * PUT:更新伺服器上的數據。 ``` axios.put(url, data, config); ``` * DELETE:刪除伺服器上的數據。 ``` axios.delete(url, config); ``` * PATCH:部分更新伺服器上的數據。 ``` axios.patch(url, data, config); ``` ## Axios Response 結構 上文中有提到,獲得 `response` 之後,可以在 `.then` 中獲得以下的資訊,可以參考下面範例: <iframe height="300" style="width: 100%;" scrolling="no" title="axios 範例 (1)" src="https://codepen.io/yen-kg/embed/jOjOveV?default-tab=html%2Cresult" frameborder="no" loading="lazy" allowtransparency="true" allowfullscreen="true"> See the Pen <a href="https://codepen.io/yen-kg/pen/jOjOveV"> axios 範例 (1)</a> by Yen Kg (<a href="https://codepen.io/yen-kg">@yen-kg</a>) on <a href="https://codepen.io">CodePen</a>. </iframe> ## Axios config 配置 ### 預設配置 透過預設配置,可以對每個請求設定一個共用的配置,例如:伺服器位址、`Header` 抬頭等。 可以參考範例: <iframe height="300" style="width: 100%;" scrolling="no" title="axios 範例 (1)" src="https://codepen.io/yen-kg/embed/YzozJKo?default-tab=html%2Cresult" frameborder="no" loading="lazy" allowtransparency="true" allowfullscreen="true"> See the Pen <a href="https://codepen.io/yen-kg/pen/YzozJKo"> axios 範例 (1)</a> by Yen Kg (<a href="https://codepen.io/yen-kg">@yen-kg</a>) on <a href="https://codepen.io">CodePen</a>. </iframe> --- 題目 --- 操作 [這個模板](https://codepen.io/yen-kg/pen/mdZdzwV?editors=0010) 執行以下要求(只能操作 script 的部分): * 利用 `axios.get` 的方法撈取「高雄旅遊網的資料」的資料,並且將回傳的資料利用 `console.log` 呈現出來即可。 * 利用 `catch` 的方式去接收錯誤訊息。 ## 回報流程 將答案寫在 CodePen 並複製 CodePen 連結貼至底下回報就算完成了喔! 解答位置請參考下圖(需打開程式碼的部分觀看) ![](https://i.imgur.com/vftL5i0.png) <!-- 解答: axios.get(api).then((res) => { console.log(res); }) .catch(error => { console.error('Error:', error); }); --> 回報區 --- | Discord | CodePen / 答案 | |:----------------:|:-------------------------------------------------------------------:| | thchen2002 | [CodePen](https://codepen.io/thchen2002/pen/azvKGvy) | | Candace | [CodePen](https://codepen.io/Candace802/pen/ZYbRoQB?editors=0012) | | dPi | [CodePen](https://codepen.io/snijqlte-the-bold/pen/WbQyJJw?editors=0011) | | 地瓜 | [CodePen](https://codepen.io/huangtzuchin/pen/ogjydyQ?editors=0011) | | William Hsieh | [CodePen](https://codepen.io/lsaimqxa-the-vuer/pen/KwdeRxa?editors=0012) | | 阿鵝 | [CodePen](https://codepen.io/noracami/pen/GgpGBJM) | | poyi | [CodePen](https://codepen.io/poyi-the-flexboxer/pen/xbwzJww?editors=0012) | | RUDY | [CodePen](https://codepen.io/Rudy-crw/pen/ByoVObj?editors=0012) | | wei_0982 | [CodePen](https://codepen.io/nico-lai/pen/YPyvRXN) | | yu005620 | [CodePen](https://codepen.io/yu-chin-chiang/pen/NPGzeJr?editors=0011) | | DaRon | [CodePen](https://codepen.io/daron0811/pen/dPYKaYB) | | Sam.S.T.Y | [CodePen](https://codepen.io/Sam-Yang-the-animator/pen/WbQymZL?editors=0110) | | Loder | [CodePen](https://codepen.io/rgbkomhs-the-flexboxer/pen/GgpGLQa?editors=0010) | | Lin | [CodePen](https://codepen.io/Lin4611/pen/RNWJmaN?editors=0010) | | Eden | [Codepen](https://codepen.io/iseden/pen/yyYqebJ) | | 登登登 | [Codepen](https://codepen.io/Duncanin/pen/YPyjwJB?editors=0011) | | Clarence | [Codepen](https://codepen.io/Clarence-Lin/pen/WbQKYpy) | | 叮咚 | [Codepen](https://codepen.io/pinchieh-lin/pen/YPyjRdx?editors=0011) | | dean | [Codepen](https://codepen.io/ch933114/pen/xbwyEqE?editors=0011) | | 黛西 | [CodePen](https://codepen.io/ChiashengLin/pen/NPGOLbO?editors=0010) | | 阿Kai | [CodePen](https://codepen.io/kaihuang3013/pen/PwPyrpz?editors=0011) | | Chen | [CodePen](https://codepen.io/JGM-C/pen/zxvMPvj?editors=0012) | |Joe|[CodePen](https://codepen.io/wasp33/pen/xbwQvvR?editors=0011)| |Momoze|[CodePen](https://codepen.io/mleczmam-the-typescripter/pen/GgpPEzo?editors=0012)| <!-- | user | [CodePen]() | -->

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully