Hướng dẫn decode nodejs - giải mã nodejs

Nội dung bài viết

Video học lập trình mỗi ngày

Decode và encode 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 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
7 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
8 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
7 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
8 String URL và
const querystring = require('querystring');

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

// Encode query string
const encodedQuery = querystring.stringify({ q: 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 trong ứng dụng Node.js.

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

URL encode

Trong Node.js 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
7 đó là
const querystring = require('querystring');

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

// Encode query string
const encodedQuery = querystring.stringify({ q: 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
3 và
const querystring = require('querystring');

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

// Encode query string
const encodedQuery = querystring.stringify({ q: 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 để mã hoá một URL. Vậy nó dùng như thế nào?

encodeURI javascript

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

const querystring = require('querystring');

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

// Encode query string
const encodedQuery = querystring.stringify({ q: 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
3, phương thức này không hỗ trợ các ký tự sau:
const querystring = require('querystring');

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

// Encode query string
const encodedQuery = querystring.stringify({ q: 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. Ví dụ sau cho 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

encodeURIComponent

Nếu như dùng

const querystring = require('querystring');

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

// Encode query string
const encodedQuery = querystring.stringify({ q: 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
3 để mã hoá một URL thì việc sử dụng
const querystring = require('querystring');

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

// Encode query string
const encodedQuery = querystring.stringify({ q: 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 thì chỉ mã hoá
const querystring = require('querystring');

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

// Encode query string
const encodedQuery = querystring.stringify({ q: 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 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 querystring = require('querystring');

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

// Encode query string
const encodedQuery = querystring.stringify({ q: 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 thì chúng tôi cung câp thêm cho bạn một module tương tự nhưng nó được tích hợp sẵn trong Node.js để mã hoá URL đó là
const encodedUrl = 'http://example.com/!Learn%20Node$/%20Example';

// Decode URL
const url = decodeURI(encodedUrl);

// Print URL
console.log(url);

// http://example.com/!Learn Node$/ Example
1.

querystring module

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

const encodedUrl = 'http://example.com/!Learn%20Node$/%20Example';

// Decode URL
const url = decodeURI(encodedUrl);

// Print URL
console.log(url);

// http://example.com/!Learn Node$/ Example
1 được tích hợp Node.js để mã hóa URL. Module 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 querystring = require('querystring');

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

// Encode query string
const encodedQuery = querystring.stringify({ q: 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

decode URL

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

const encodedUrl = 'http://example.com/!Learn%20Node$/%20Example';

// Decode URL
const url = decodeURI(encodedUrl);

// Print URL
console.log(url);

// http://example.com/!Learn Node$/ Example
3. Đương nhiên, khi mã hoá thì phải có giải mã, tương tự khi encode xong thi phải decode.

Tương tự thôi

const encodedUrl = 'http://example.com/!Learn%20Node$/%20Example';

// Decode URL
const url = decodeURI(encodedUrl);

// Print URL
console.log(url);

// http://example.com/!Learn Node$/ Example
4 cũng cung cấp cho hai methods đó là
const encodedUrl = 'http://example.com/!Learn%20Node$/%20Example';

// Decode URL
const url = decodeURI(encodedUrl);

// Print URL
console.log(url);

// http://example.com/!Learn Node$/ Example
5 và
const encodedUrl = 'http://example.com/!Learn%20Node$/%20Example';

// Decode URL
const url = decodeURI(encodedUrl);

// Print URL
console.log(url);

// http://example.com/!Learn Node$/ Example
6 để thực hiện giải mã URL.

decodeURI javascript

Phương pháp

const encodedUrl = 'http://example.com/!Learn%20Node$/%20Example';

// Decode URL
const url = decodeURI(encodedUrl);

// Print URL
console.log(url);

// http://example.com/!Learn Node$/ Example
5 được sử dụng để giải mã một URL đầy đủ:

const encodedUrl = 'http://example.com/!Learn%20Node$/%20Example';

// Decode URL
const url = decodeURI(encodedUrl);

// Print URL
console.log(url);

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

decodeURIComponent

Chức năng

const encodedUrl = 'http://example.com/!Learn%20Node$/%20Example';

// Decode URL
const url = decodeURI(encodedUrl);

// Print URL
console.log(url);

// http://example.com/!Learn Node$/ Example
6 được sử dụng để các thành phần URL giải mã:

try {
  var a = decodeURIComponent('%E0%A4%A');
} catch(e) {
  console.error(e);
}

// URIError: malformed URI sequence

Lời kết

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

const encodedUrl = 'http://example.com/!Learn%20Node$/%20Example';

// Decode URL
const url = decodeURI(encodedUrl);

// Print URL
console.log(url);

// http://example.com/!Learn Node$/ Example
9 rồi, cho nên việc tóm tắt là việc làm dư thừa. Nhưng các bạn cũng nên để ý việc khác nhau giữa
const querystring = require('querystring');

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

// Encode query string
const encodedQuery = querystring.stringify({ q: 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
3 và
const querystring = require('querystring');

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

// Encode query string
const encodedQuery = querystring.stringify({ q: 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, cũng như
const encodedUrl = 'http://example.com/!Learn%20Node$/%20Example';

// Decode URL
const url = decodeURI(encodedUrl);

// Print URL
console.log(url);

// http://example.com/!Learn Node$/ Example
5 và
const encodedUrl = 'http://example.com/!Learn%20Node$/%20Example';

// Decode URL
const url = decodeURI(encodedUrl);

// Print URL
console.log(url);

// http://example.com/!Learn Node$/ Example
6 trong Node.js. Tránh nhầm lẫn để sử dụng không đúng mục đích của việc làm của mình.

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

URL encode

Phương thức 

const encodedUrl = 'http://example.com/!Learn%20Node$/%20Example';

// Decode URL
const url = decodeURI(encodedUrl);

// Print URL
console.log(url);

// http://example.com/!Learn Node$/ Example
5 sẽ giải mã một chuỗi uri đã được mã hóa các kí tự đặc biệt trước đó. Phương thức trả về chuỗi đã được giải mã.uri đã được mã hóa các kí tự đặc biệt trước đó. Phương thức trả về chuỗi đã được giải mã.

Nội dung chính

  • Cách sử dụng
  • 1- Javascript URL Encoding
  • 2- encodeURI(), decodeURI()
  • 3- encodeURIComponent(), decodeURIComponent()
  • 4- Encode all characters?

Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.freetuts.net, không được copy dưới mọi hình thức.

Chuỗi uri mã hóa truyền vào chính là kết quả trả về khi thực hiện phương thức 

const querystring = require('querystring');

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

// Encode query string
const encodedQuery = querystring.stringify({ q: 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
3.uri mã hóa truyền vào chính là kết quả trả về khi thực hiện phương thức 
const querystring = require('querystring');

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

// Encode query string
const encodedQuery = querystring.stringify({ q: 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
3.

Cú pháp

Cú pháp: 

try {
  var a = decodeURIComponent('%E0%A4%A');
} catch(e) {
  console.error(e);
}

// URIError: malformed URI sequence
6
try {
  var a = decodeURIComponent('%E0%A4%A');
} catch(e) {
  console.error(e);
}

// URIError: malformed URI sequence
6

Trong đó::

Bài viết này được đăng tại [free tuts .net]

  • uri là chuỗi đã được mã hóa, thường thì uri là kết quả trả về của phương thức 
    const querystring = require('querystring');
    
    const baseUrl = 'http://example.com/search?';
    const query = 'SELECT * from users WHERE id = 1'
    
    // Encode query string
    const encodedQuery = querystring.stringify({ q: 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
    
    3.
    là chuỗi đã được mã hóa, thường thì uri là kết quả trả về của phương thức 
    const querystring = require('querystring');
    
    const baseUrl = 'http://example.com/search?';
    const query = 'SELECT * from users WHERE id = 1'
    
    // Encode query string
    const encodedQuery = querystring.stringify({ q: 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
    
    3.

Cách sử dụng

Sử dụng phương thức 

const encodedUrl = 'http://example.com/!Learn%20Node$/%20Example';

// Decode URL
const url = decodeURI(encodedUrl);

// Print URL
console.log(url);

// http://example.com/!Learn Node$/ Example
5 để giải mã chuỗi uri.



    
        
    
    
        

Học lập trình miễn phí tại freetuts.net

Kết quả

EnCode: freetuts.net/h%C3%89c-js%E2%84%A2%C2%A9%C2%A3
DeCode: freetuts.net/hÉc-js™©£

Tham khảo: w3schools.com

Nhóm phát triển của chúng tôi vừa ra mắt website langlearning.net học tiếng Anh, Nga, Đức, Pháp, Việt, Trung, Hàn, Nhật, ... miễn phí cho tất cả mọi người. Là một website được viết trên công nghệ web Flutter vì vậy hỗ trợ rất tốt cho người học, kể cả những người học khó tính nhất. Hiện tại website đang tiếp tục được cập nhập nội dung cho phong phú và đầy đủ hơn. Mong các bạn nghé thăm và ủng hộ website mới của chúng tôi. Hãy theo dõi chúng tôi trên Fanpage để nhận được thông báo mỗi khi có bài viết mới. Facebook
Là một website được viết trên công nghệ web Flutter vì vậy hỗ trợ rất tốt cho người học, kể cả những người học khó tính nhất.
Hiện tại website đang tiếp tục được cập nhập nội dung cho phong phú và đầy đủ hơn. Mong các bạn nghé thăm và ủng hộ website mới của chúng tôi. Hãy theo dõi chúng tôi trên Fanpage để nhận được thông báo mỗi khi có bài viết mới. Facebook

1- Javascript URL Encoding

Trong bài viết về "HTML URL Encoding" tôi có giải thích vì sao bạn cần phải mã hóa (encode) một URL, và bảng các ký tự và giá trị mã hóa của chúng. "HTML URL Encoding" tôi có giải thích vì sao bạn cần phải mã hóa (encode) một URL, và bảng các ký tự và giá trị mã hóa của chúng.

Trong bài viết này tôi sẽ chỉ cho bạn làm thế nào để mã hóa (encode), và giải mã hóa (decode) một URL với Javascript. URL với Javascript.

Chúng ta bắt đầu với câu hỏi "Làm thế nào để mã hóa (encode) một URL với Javascript?". "Làm thế nào để mã hóa (encode) một URL với Javascript?".

Nó thực sự phụ thuộc vào cái mà bạn muốn làm. Javascript cung cấp cho bạn 2 hàm đó là encodeURI() và encodeURIComponent(). Sự khác biệt giữa 2 hàm này là các ký tự nào sẽ được chúng mã hóa. Javascript cung cấp cho bạn 2 hàm đó là encodeURI()encodeURIComponent(). Sự khác biệt giữa 2 hàm này là các ký tự nào sẽ được chúng mã hóa.

  • Hàm encodeURI() mã hóa mọi ký tự ngoại trừ các ký tự [email protected]#$&*()=:/,;?+encodeURI() mã hóa mọi ký tự ngoại trừ các ký tự [email protected]#$&*()=:/,;?+
  • Hàm encodeURIComponent() mã hóa mọi ký tự ngoại trừ các ký tự -_.!~*'()encodeURIComponent() mã hóa mọi ký tự ngoại trừ các ký tự -_.!~*'()

Do vậy hàm encodeURI() phù hợp nếu bạn muốn mã hóa toàn bộ một URL, vì phần như ( http:// ) sẽ không bị mã hóa. Sử dụng hàm encodeURIComponent() nếu bạn muốn mã hóa giá trị của một tham số. encodeURI() phù hợp nếu bạn muốn mã hóa toàn bộ một URL, vì phần như ( http:// ) sẽ không bị mã hóa. Sử dụng hàm encodeURIComponent() nếu bạn muốn mã hóa giá trị của một tham số.

Nếu bạn sử dụng hàm encodeURIComponent() để mã hóa toàn bộ một URL bạn sẽ nhận được một kết quả không mong muốn. encodeURIComponent() để mã hóa toàn bộ một URL bạn sẽ nhận được một kết quả không mong muốn.


encodeURI("http://example.com/ hey!/")
// http://example.com/%20hey!/


encodeURIComponent("http://www.example.com/ hey!")
//  http%3A%2F%2Fexample.com%2F%20hey!%2F
 

var set1 = ";,/?:@&=+$#";  // Reserved Characters
var set2 = "-_.!~*'()";    // Unreserved Marks
var set3 = "ABC abc 123";  // Alphanumeric Characters + Space

console.log(encodeURI(set1)); // ;,/?:@&=+$#
console.log(encodeURI(set2)); // -_.!~*'()
console.log(encodeURI(set3)); // ABC%20abc%20123 (the space gets encoded as %20)

console.log(encodeURIComponent(set1)); // %3B%2C%2F%3F%3A%40%26%3D%2B%24%23
console.log(encodeURIComponent(set2)); // -_.!~*'()
console.log(encodeURIComponent(set3)); // ABC%20abc%20123 (the space gets encoded as %20)

Để giải mã hóa (decode) Javascript cung cấp cho bạn 2 hàm decodeURI() và decodeURIComponent(), tình huống sử dụng của chúng tương ứng giống với 2 hàm encodeURI() và encodeURLComponent(). Javascript cung cấp cho bạn 2 hàm decodeURI()decodeURIComponent(), tình huống sử dụng của chúng tương ứng giống với 2 hàm encodeURI()encodeURLComponent().

2- encodeURI(), decodeURI()

Hàm encodeURI(uri) trả về một chuỗi (string), là kết quả của việc mã hóa (encode) tham số uri. encodeURI(uri) trả về một chuỗi (string), là kết quả của việc mã hóa (encode) tham số uri.

Hàm encodeURI() mã hóa (encode) tất cả các ký tự, ngoại trừ các ký tự dưới đây: encodeURI() mã hóa (encode) tất cả các ký tự, ngoại trừ các ký tự dưới đây:


A-Z a-z 0-9 ; , / ? : @ & = + $ - _ . ! ~ * ' ( ) #

Hàm encodeURI(uri) được sử dụng nếu bạn muốn mã hóa toàn bộ một URL hoặc một phần URL. encodeURI(uri) được sử dụng nếu bạn muốn mã hóa toàn bộ một URL hoặc một phần URL.

encodeURI-example.html

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

Ví dụ sử dụng hàm decodeURI(): decodeURI():

decodeURI-example.js

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

3- encodeURIComponent(), decodeURIComponent()

Hàm encodeURIComponent(uri) trả về một chuỗi (string), là kết quả của việc mã hóa (encode) tham số uri. encodeURIComponent(uri) trả về một chuỗi (string), là kết quả của việc mã hóa (encode) tham số uri.

Hàm encodeURIComponent() mã hóa (encode) tất cả các ký tự, ngoại trừ các ký tự dưới đây: encodeURIComponent() mã hóa (encode) tất cả các ký tự, ngoại trừ các ký tự dưới đây:

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

Hàm encodeURIComponent(uri) thường được sử dụng để mã hóa (encode) giá trị của một tham số của URL. encodeURIComponent(uri) thường được sử dụng để mã hóa (encode) giá trị của một tham số của URL.

encodeURIComponent-example.html

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
3

Ví dụ sử dụng hàm decodeURIComponent(): decodeURIComponent():

decodeURIComponent-example.js

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

4- Encode all characters?

Cả hai hàm encodeURI() và encodeURIComponent() đều không mã hóa tất cả các ký tự. Và Javascript cũng không có một hàm nào khác hỗ trợ việc này, vì vậy nếu muốn bạn phải viết một hàm của riêng mình.encodeURI()encodeURIComponent() đều không mã hóa tất cả các ký tự. Và Javascript cũng không có một hàm nào khác hỗ trợ việc này, vì vậy nếu muốn bạn phải viết một hàm của riêng mình.

encodeURIAll.js

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
5

encodeURIAll-example.html

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