zuppachu / Joanne-s-Learning-Blog

程式導師實驗計畫第二期 之 Coding 學習紀錄。
2 stars 0 forks source link

[ MTR01 ] - Lesson 4-2 之 HTTP 基礎與 Ajax 筆記 #6

Open zuppachu opened 5 years ago

zuppachu commented 5 years ago

Request 與 Response

  1. 都包含 Header 跟 Body Header 包含一些額外的資訊,Body 則是主要內容
  2. 遵循一定的標準格式 每個跟電腦相關的東西都有標準

標準化很重要

有規格、標準後,才能被機器處理!

Request

在 Request Header 內可以看到:

  1. Method URL HTTP/version
  2. Host, Referer...各種屬性

Response:

在 Response Header 裡面可以看到:

  1. HTTP/1.1 status-code 描述
    第一行: HTTP/1.1 200 ok
  2. Server, Cache-Control, X-xxx...各種屬性
    前面 X 開頭的 header 是自訂的 response header, 不屬於標準規範。
  3. body 就是網頁內容

HTTP Request Methods

GET:最常見的 method,從伺服器端拿資料(例如:看網頁) POST: 最常見的 method,從客戶端送資料去伺服器端(如:登入) PATCH:增加或部分更改一筆新的資料 PUT:把整個資料都換新的 DELETE:刪除資料 OPTIONS:看伺服器支援哪些 method HEAD:跟 GET 一樣,差別在於HEAD 的 response 沒有 body,只會取得 「HTTP header 資料」。

HTTP Response Status Codes

以下為常見的 status codes:

200 OK 301 Moved Permanently(永久轉址)

Get a.com
status code: 301
Location: b.com
// 下次用瀏覽器去 a.com ,瀏覽器不會再對 a.com 發送 request,而是直接帶去 b.com (因為知道永久移址)

302 Found(暫時轉址)

Get a.com
status code: 302
Location: b.com
// 瀏覽器則是還會再問一次是否要去 b.com

400 Bad Request(client 端有錯誤): 字太醜的例子,千千看不懂~ 403 Forbidden(client 端沒有權限) 404 Not Found (崔無) 500 Internal Server Error (伺服器出了問題) 503 Service Unavailable(常出現在搶票頁面)

代碼意義:

1xx - 等待 (Hold on)
2xx - 成功 (here you go)
3xx - 轉址  (Go away)
4xx - client 端有錯誤 (You fucked up)
5xx - server 端有錯誤(I fucked up)

前端跟後端溝通的方法(怎麼從前端發 request ?)

引入:

  1. <script src='...'/>

    影片 20:40 處

股溝追蹤 GA(Google Analytics): 在網頁上放一個股溝 Githubissues.

  • Githubissues is a development platform for aggregating issues.