Nhận phản hồi html javascript

Giao thức truyền tải siêu văn bản (HTTP) được thiết kế để cho phép liên lạc giữa máy khách và máy chủ

HTTP hoạt động như một giao thức phản hồi yêu cầu giữa máy khách và máy chủ

Ví dụ. Máy khách (trình duyệt) gửi yêu cầu HTTP đến máy chủ; . Phản hồi chứa thông tin trạng thái về yêu cầu và cũng có thể chứa nội dung được yêu cầu


  • ĐƯỢC
  • BƯU KIỆN
  • ĐẶT
  • CÁI ĐẦU
  • XÓA BỎ
  • TÙY CHỌN
  • LIÊN KẾT
  • DẤU VẾT

Hai phương thức HTTP phổ biến nhất là. NHẬN và ĐĂNG


Phương thức NHẬN

GET được sử dụng để yêu cầu dữ liệu từ một tài nguyên được chỉ định

Lưu ý rằng chuỗi truy vấn (cặp tên/giá trị) được gửi trong URL của yêu cầu GET

/test/demo_form. php?name1=value1&name2=value2

Một số lưu ý về yêu cầu GET

  • Yêu cầu GET có thể được lưu trữ
  • Yêu cầu GET vẫn còn trong lịch sử trình duyệt
  • Yêu cầu GET có thể được đánh dấu
  • Yêu cầu GET không bao giờ được sử dụng khi xử lý dữ liệu nhạy cảm
  • Yêu cầu GET có giới hạn độ dài
  • Yêu cầu GET chỉ được sử dụng để yêu cầu dữ liệu (không sửa đổi)

Phương thức ĐĂNG

POST được sử dụng để gửi dữ liệu đến máy chủ để tạo/cập nhật tài nguyên

Dữ liệu được gửi đến máy chủ bằng POST được lưu trữ trong phần thân yêu cầu của yêu cầu HTTP

ĐĂNG /test/demo_form. php HTTP/1. 1
Máy chủ. w3schools. com

name1=value1&name2=value2

Một số lưu ý về yêu cầu POST

  • Yêu cầu POST không bao giờ được lưu trữ
  • Yêu cầu POST không còn trong lịch sử trình duyệt
  • Yêu cầu POST không thể được đánh dấu
  • Yêu cầu POST không có hạn chế về độ dài dữ liệu

So sánh NHẬN với. BƯU KIỆN

Bảng sau đây so sánh hai phương thức HTTP. NHẬN và ĐĂNG

Nút GETPOSTBACK/ReloadHarmlessDữ liệu sẽ được gửi lại (trình duyệt sẽ thông báo cho người dùng rằng dữ liệu sắp được gửi lại) Đã đánh dấu Có thể đánh dấu Không thể đánh dấu Đã lưu vào bộ đệm Có thể lưu vào bộ đệm Không lưu vào bộ đệm Loại ứng dụng mã hóa/x-www-form-urlencodedapplication/x-www-form . Sử dụng mã hóa nhiều phần cho dữ liệu nhị phânLịch sửCác tham số vẫn còn trong lịch sử trình duyệtCác tham số không được lưu trong lịch sử trình duyệtHạn chế về độ dài dữ liệuCó, khi gửi dữ liệu, phương thức GET sẽ thêm dữ liệu vào URL; . Dữ liệu nhị phân cũng được phépSecurityGET kém an toàn hơn so với POST vì dữ liệu được gửi là một phần của URL

Không bao giờ sử dụng GET khi gửi mật khẩu hoặc thông tin nhạy cảm khác

POST an toàn hơn một chút so với GET vì các tham số không được lưu trữ trong lịch sử trình duyệt hoặc trong nhật ký máy chủ webVisibilityData hiển thị cho mọi người trong URLDữ liệu không được hiển thị trong URL



Phương pháp PUT

PUT được sử dụng để gửi dữ liệu đến máy chủ để tạo/cập nhật tài nguyên

Sự khác biệt giữa POST và PUT là các yêu cầu PUT là bình thường. Nghĩa là, gọi cùng một yêu cầu PUT nhiều lần sẽ luôn tạo ra cùng một kết quả. Ngược lại, gọi một yêu cầu POST liên tục có tác dụng phụ là tạo cùng một tài nguyên nhiều lần


Phương pháp HEAD

HEAD gần giống với GET, nhưng không có phần thân phản hồi

Nói cách khác, nếu GET /users trả về danh sách người dùng, thì HEAD /users sẽ đưa ra yêu cầu tương tự nhưng sẽ không trả về danh sách người dùng

Các yêu cầu HEAD rất hữu ích để kiểm tra xem yêu cầu GET sẽ trả về cái gì trước khi thực sự thực hiện yêu cầu GET - như trước khi tải xuống một tệp lớn hoặc nội dung phản hồi


Phương thức XÓA

Phương thức DELETE xóa tài nguyên đã chỉ định


Phương pháp PATCH

Phương thức PATCH được sử dụng để áp dụng các sửa đổi một phần cho tài nguyên


Phương pháp TÙY CHỌN

Phương thức TÙY CHỌN mô tả các tùy chọn giao tiếp cho tài nguyên đích


Phương thức KẾT NỐI

Phương thức CONNECT được sử dụng để bắt đầu liên lạc hai chiều (đường hầm) với tài nguyên được yêu cầu


Phương pháp TRACE

Phương pháp TRACE được sử dụng để thực hiện kiểm tra lặp lại thông báo kiểm tra đường dẫn cho tài nguyên đích (hữu ích cho mục đích gỡ lỗi)

Có một thuật ngữ ô “AJAX” (viết tắt là JavaScript và XML không đồng bộ) cho các yêu cầu mạng từ JavaScript. Mặc dù vậy, chúng tôi không phải sử dụng XML. thuật ngữ này xuất phát từ thời xa xưa, đó là lý do tại sao từ đó có. Bạn có thể đã nghe thuật ngữ đó rồi

Có nhiều cách để gửi yêu cầu mạng và nhận thông tin từ máy chủ

Phương pháp

let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
4 hiện đại và linh hoạt, vì vậy chúng ta sẽ bắt đầu với nó. Nó không được hỗ trợ bởi các trình duyệt cũ (có thể được điền đầy đủ), nhưng được hỗ trợ rất tốt trong số các trình duyệt hiện đại

Cú pháp cơ bản là

let promise = fetch(url, [options])

  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    5 – URL để truy cập
  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    6 – tham số tùy chọn. phương thức, tiêu đề, v.v.

Không có

let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
6, đây là một yêu cầu GET đơn giản, tải xuống nội dung của
let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
5

Trình duyệt bắt đầu yêu cầu ngay lập tức và trả về lời hứa rằng mã gọi sẽ sử dụng để nhận kết quả

Nhận phản hồi thường là một quá trình gồm hai giai đoạn

Đầu tiên,

let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
9, được trả về bởi
let url = 'https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits';
let response = await fetch(url);

let commits = await response.json(); // read response body and parse as JSON

alert(commits[0].author.login);
0, giải quyết bằng một đối tượng của lớp tích hợp ngay khi máy chủ phản hồi với các tiêu đề

Ở giai đoạn này, chúng ta có thể kiểm tra trạng thái HTTP, để xem nó có thành công hay không, hãy kiểm tra các tiêu đề, nhưng chưa có phần thân

Lời hứa bị từ chối nếu

let url = 'https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits';
let response = await fetch(url);

let commits = await response.json(); // read response body and parse as JSON

alert(commits[0].author.login);
0 không thể thực hiện yêu cầu HTTP, e. g. sự cố mạng hoặc không có trang web nào như vậy. Trạng thái HTTP bất thường, chẳng hạn như 404 hoặc 500 không gây ra lỗi

Chúng ta có thể thấy trạng thái HTTP trong thuộc tính phản hồi

  • let url = 'https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits';
    let response = await fetch(url);
    
    let commits = await response.json(); // read response body and parse as JSON
    
    alert(commits[0].author.login);
    2 – Mã trạng thái HTTP, e. g. 200
  • let url = 'https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits';
    let response = await fetch(url);
    
    let commits = await response.json(); // read response body and parse as JSON
    
    alert(commits[0].author.login);
    3 – boolean,
    let url = 'https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits';
    let response = await fetch(url);
    
    let commits = await response.json(); // read response body and parse as JSON
    
    alert(commits[0].author.login);
    4 nếu mã trạng thái HTTP là 200-299

Ví dụ

let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}

Thứ hai, để lấy nội dung phản hồi, chúng ta cần sử dụng một lệnh gọi phương thức bổ sung

let url = 'https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits';
let response = await fetch(url);

let commits = await response.json(); // read response body and parse as JSON

alert(commits[0].author.login);
5 cung cấp nhiều phương thức dựa trên lời hứa để truy cập phần thân ở nhiều định dạng khác nhau

  • let url = 'https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits';
    let response = await fetch(url);
    
    let commits = await response.json(); // read response body and parse as JSON
    
    alert(commits[0].author.login);
    6 – đọc phản hồi và gửi lại dưới dạng văn bản,
  • let url = 'https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits';
    let response = await fetch(url);
    
    let commits = await response.json(); // read response body and parse as JSON
    
    alert(commits[0].author.login);
    7 – phân tích phản hồi dưới dạng JSON,
  • let url = 'https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits';
    let response = await fetch(url);
    
    let commits = await response.json(); // read response body and parse as JSON
    
    alert(commits[0].author.login);
    8 – trả về phản hồi dưới dạng đối tượng
    let url = 'https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits';
    let response = await fetch(url);
    
    let commits = await response.json(); // read response body and parse as JSON
    
    alert(commits[0].author.login);
    9 (được giải thích trong chương tiếp theo),
  • let promise = fetch(url, [options])
    80 – trả về phản hồi dưới dạng Blob (dữ liệu nhị phân có loại),
  • let promise = fetch(url, [options])
    81 – trả về phản hồi dưới dạng ArrayBuffer (biểu diễn dữ liệu nhị phân ở mức độ thấp),
  • Ngoài ra,
    let promise = fetch(url, [options])
    82 là một đối tượng, nó cho phép bạn đọc từng đoạn của phần thân, chúng ta sẽ xem một ví dụ sau

Chẳng hạn, hãy lấy một đối tượng JSON với các cam kết mới nhất từ ​​GitHub

let url = 'https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits';
let response = await fetch(url);

let commits = await response.json(); // read response body and parse as JSON

alert(commits[0].author.login);

Hoặc, tương tự nếu không có

let promise = fetch(url, [options])
83, sử dụng cú pháp lời hứa thuần túy

let promise = fetch(url, [options])
8

Để có được văn bản phản hồi,

let promise = fetch(url, [options])
84 thay vì
let promise = fetch(url, [options])
85

let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
6

Là một trường hợp trưng bày để đọc ở định dạng nhị phân, hãy tìm nạp và hiển thị hình ảnh biểu trưng của thông số kỹ thuật "tìm nạp" (xem chương Blob để biết chi tiết về các hoạt động trên

let promise = fetch(url, [options])
86)

let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
8

Quan trọng

Chúng tôi chỉ có thể chọn một phương pháp đọc cơ thể

Nếu chúng tôi đã nhận được phản hồi với

let url = 'https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits';
let response = await fetch(url);

let commits = await response.json(); // read response body and parse as JSON

alert(commits[0].author.login);
6, thì
let url = 'https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits';
let response = await fetch(url);

let commits = await response.json(); // read response body and parse as JSON

alert(commits[0].author.login);
7 sẽ không hoạt động vì nội dung cơ thể đã được xử lý

let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
1

Các tiêu đề phản hồi có sẵn trong một đối tượng tiêu đề giống như Bản đồ trong

let promise = fetch(url, [options])
89

Nó không chính xác là một Bản đồ, nhưng nó có các phương thức tương tự để lấy các tiêu đề riêng lẻ theo tên hoặc lặp lại chúng

let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
3

Để đặt tiêu đề yêu cầu trong

let url = 'https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits';
let response = await fetch(url);

let commits = await response.json(); // read response body and parse as JSON

alert(commits[0].author.login);
0, chúng ta có thể sử dụng tùy chọn
let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
61. Nó có một đối tượng với các tiêu đề gửi đi, như thế này

let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
6

…Nhưng có một danh sách mà chúng ta không thể thiết lập

  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    62,
    let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    63
  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    64
  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    65
  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    66
  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    67
  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    68,
    let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    69
  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    80
  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    81
  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    82
  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    83
  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    84
  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    85
  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    86
  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    87
  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    88
  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    89
  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    10
  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    11
  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    12
  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    13

Các tiêu đề này đảm bảo HTTP phù hợp và an toàn, vì vậy chúng được kiểm soát độc quyền bởi trình duyệt

Để thực hiện yêu cầu

let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
14 hoặc yêu cầu bằng phương pháp khác, chúng tôi cần sử dụng tùy chọn
let url = 'https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits';
let response = await fetch(url);

let commits = await response.json(); // read response body and parse as JSON

alert(commits[0].author.login);
0

  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    16 – Phương thức HTTP, e. g.
    let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    14,
  • let response = await fetch(url);
    
    if (response.ok) { // if HTTP-status is 200-299
      // get the response body (the method explained below)
      let json = await response.json();
    } else {
      alert("HTTP-Error: " + response.status);
    }
    18 – cơ quan yêu cầu, một trong những
    • một chuỗi (e. g. được mã hóa JSON),
    • Đối tượng
      let url = 'https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits';
      let response = await fetch(url);
      
      let commits = await response.json(); // read response body and parse as JSON
      
      alert(commits[0].author.login);
      9, để gửi dữ liệu dưới dạng
      let response = await fetch(url);
      
      if (response.ok) { // if HTTP-status is 200-299
        // get the response body (the method explained below)
        let json = await response.json();
      } else {
        alert("HTTP-Error: " + response.status);
      }
      30,
    • let promise = fetch(url, [options])
      86/
      let response = await fetch(url);
      
      if (response.ok) { // if HTTP-status is 200-299
        // get the response body (the method explained below)
        let json = await response.json();
      } else {
        alert("HTTP-Error: " + response.status);
      }
      32 để gửi dữ liệu nhị phân,
    • URLSearchParams, để gửi dữ liệu dưới dạng mã hóa
      let response = await fetch(url);
      
      if (response.ok) { // if HTTP-status is 200-299
        // get the response body (the method explained below)
        let json = await response.json();
      } else {
        alert("HTTP-Error: " + response.status);
      }
      33, hiếm khi được sử dụng

Định dạng JSON được sử dụng hầu hết thời gian

Ví dụ: mã này gửi đối tượng

let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
34 dưới dạng JSON

let url = 'https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits';
let response = await fetch(url);

let commits = await response.json(); // read response body and parse as JSON

alert(commits[0].author.login);
0

Xin lưu ý, nếu yêu cầu

let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
18 là một chuỗi, thì tiêu đề
let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
36 được đặt thành
let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
37 theo mặc định

Tuy nhiên, vì chúng tôi sẽ gửi JSON, thay vào đó, chúng tôi sử dụng tùy chọn

let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
61 để gửi
let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
39, đúng
let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
36 cho dữ liệu được mã hóa JSON

Chúng tôi cũng có thể gửi dữ liệu nhị phân với

let url = 'https://api.github.com/repos/javascript-tutorial/en.javascript.info/commits';
let response = await fetch(url);

let commits = await response.json(); // read response body and parse as JSON

alert(commits[0].author.login);
0 bằng cách sử dụng các đối tượng
let promise = fetch(url, [options])
86 hoặc
let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
32

Trong ví dụ này, có một

let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
64 mà chúng ta có thể vẽ bằng cách di chuột qua nó. Một cú nhấp chuột vào nút “gửi” sẽ gửi hình ảnh đến máy chủ

let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
0

Xin lưu ý, ở đây chúng tôi không đặt tiêu đề

let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
36 theo cách thủ công, vì đối tượng
let promise = fetch(url, [options])
86 có loại tích hợp sẵn (ở đây là
let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
67, như được tạo bởi
let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
68). Đối với các đối tượng
let promise = fetch(url, [options])
86, loại đó trở thành giá trị của
let response = await fetch(url);

if (response.ok) { // if HTTP-status is 200-299
  // get the response body (the method explained below)
  let json = await response.json();
} else {
  alert("HTTP-Error: " + response.status);
}
36

Làm cách nào để lấy dữ liệu từ phản hồi HTML trong JavaScript?

Phương thức Tìm nạp văn bản() # . Điều này sẽ trả về một chuỗi văn bản của HTML. tìm nạp('/về'). then(function (response) { // Cuộc gọi API đã thành công. use the text() method instead of the json() method on the stream. This will return a text string of the HTML. fetch('/about'). then(function (response) { // The API call was successful!

Làm cách nào để nhận được phản hồi trong JavaScript?

Các phương thức nhận nội dung phản hồi. .
text() – trả về phản hồi dưới dạng văn bản,
json() – phân tích phản hồi dưới dạng đối tượng JSON,
formData() – trả về phản hồi dưới dạng đối tượng FormData ( mã hóa multipart/form-data, xem chương tiếp theo),
blob() – trả về phản hồi dưới dạng Blob (dữ liệu nhị phân có loại),

Làm cách nào để nhận phản hồi URL trong JavaScript?

Bước 1 — Bắt đầu với Tìm nạp cú pháp API .
tìm nạp (url). then(function() { // xử lý phản hồi })
tìm nạp (url). then(function() { // xử lý phản hồi }). catch(function() { // xử lý lỗi });
//. tìm nạp (url, tìm nạp dữ liệu). then(function() { // Xử lý phản hồi bạn nhận được từ API });

Làm cách nào để nhận giá trị từ phản hồi trong JavaScript?

Sử dụng phương thức tìm nạp() để trả về một lời hứa phân giải thành một đối tượng Phản hồi . Để lấy dữ liệu thực tế, bạn gọi một trong các phương thức của đối tượng Response e. g. , văn bản() hoặc json(). Các phương pháp này giải quyết thành dữ liệu thực tế.