Siêu liên kết trong chuỗi javascript

Giải mã và mã hóa là một trong những cách thường được sử dụng để tránh các cuộc tấn công tập lệnh trên nhiều trang web (XSS) bằng cách mã hóa các ký tự đặc biệt trong một URL. Bài viết này cung cấp cách phát triển khai cũng như giải thích rõ ràng hơn. Sử dụng

const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
6 để chuyển đổi một chuỗi thành một định dạng URL hợp lệ để làm cho dữ liệu được truyền đi đáng tin cậy và an toàn hơn. Và sử dụng
const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
7 ngược lại với quá trình mã hóa. Nó chuyển đổi các chuỗi URL đã được mã hóa và các tham số truy vấn trở lại định dạng bình thường của chúng

Trong bài viết này, bạn sẽ học cách

const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
6 hoặc
const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
7 String URL và
const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
0 trong Node ứng dụng. js

Xem thêm. mẹo viết code javascript và nodejs

mã hóa URL

Trong Node. js được xây dựng trên công cụ JavaScript V8 của Chrome Hỗ trợ hai phương pháp

const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
6 đó là
const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
1 và
const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
2 để mã hóa một URL. Vì vậy nó được sử dụng như thế nào?

mã hóa URI javascript

Nếu bạn muốn mã hóa một URL hoàn chỉnh thì có thể sử dụng

const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
1, phương thức này không hỗ trợ các ký tự sau.
const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
4. Ví dụ sau để thấy điều đó

const url = 'http://example.com/!Learn Node$/ Example';

// Encode complete URL
const encodedUrl = encodeURI(url);

// Print encoded URL
console.log(encodedUrl);

// http://example.com/!Learn%20Node$/%20Example

mã hóaURIThành phần

Nếu như dùng

const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
1 để mã hóa một URL thì việc sử dụng
const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
2 thì chỉ mã hóa
const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
7 hay còn gọi là tham số chuỗi truy vấn chứ không phải một URL hoàn chỉnh. Xem tiếp ví dụ sau

const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201

Ngoài phương pháp

const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
2 thì chúng tôi cung cấp thêm cho bạn một mô-đun tương tự nhưng nó được tích hợp sẵn trong Node. js to code URL that is
const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
9

mô-đun chuỗi truy vấn

Bạn cũng có thể sử dụng

const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
9 được tích hợp nút. js to code URL. Mô-đun này cung cấp các phương thức tiện ích để phân tích cú pháp và định dạng chuỗi truy vấn URL

const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
2

giải mã URL

Trên đó là công việc hướng dẫn cũng như giải thích về công việc mã hóa

const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
21. Đương nhiên, khi mã hóa thì phải giải mã, tương tự khi mã hóa xong thì phải giải mã

Tương tự thôi

const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
22 cũng cung cấp cho hai phương thức là
const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
23 và
const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
24 để thực hiện giải mã URL

javascript giải mãURI

Phương pháp

const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
23 được sử dụng để giải mã đầy đủ một URL

const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
8

giải mãURIThành phần

Chức năng

const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
24 được sử dụng để giải mã URL thành phần

const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
0

Lời kết

Bài viết đã xác định rõ ràng cho việc sử dụng

const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
27 rồi, cho nên công việc tóm tắt là công việc dư thừa. Nhưng các bạn cũng nên để ý đến những công việc khác nhau giữa
const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
1 và
const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
2, cũng như
const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
23 và
const baseUrl = 'http://example.com/search?q=';
const query = 'SELECT * from users WHERE id = 1';

// Encode query string
const encodedQuery = encodeURIComponent(query);

// Build full URL
const url = baseUrl + encodedQuery;

// Print full URL
console.log(url);

// http://example.com/search?q=SELECT%20*%20from%20users%20WHERE%20id%20%3D%201
24 trong Node. js. Asmising to use invalid target of the work of mình