Sitemap / Advertise

Information



Tags



Share

How to hide the query string in the URL after a GET Request in JavaScript

Advertisement:


read_later

Read Later

Keywords



Keywords



read_later

Read Later

Information

Tags

Share





Advertisement

Advertisement




Definition

In this tutorial, I will discuss how to remove the query string sent in the URL of a Get request in JavaScript.

We will use AJAX to make a GET request to send information without displaying the query string in the URL.

Syntax(1):

Note that the query string (name/value pairs) is sent in the URL of a GET request:

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

Code

📄 In jQuery, select the form elements.

📄 If the submit button is clicked, execute the function to send a Get request with the given variables by using AJAX.

📄 If the request is successful, return the page without displaying the query string (?) in the URL by removing it with the split method.

📄 window.location = window.location.href.split('?')[0]


-------------- JavaScript --------------

$("form").on("click", ".submit", function() {	
	$.ajax({
		url: '?name1=' + variable_1 + '&name2=' + variable_2,
		type: 'GET',
		success: () => {window.location = window.location.href.split('?')[0]}
	});
});

References

(1) https://www.w3schools.com/tags/ref_httpmethods.asp