Comparison of Several Different Methods of Passing Parameters in JS

  • 2021-07-13 03:44:50
  • OfStack

When developing web pages, it is an inevitable problem to interact with foreground and background data. Different business needs have different transmission methods. The following are several transmission methods that I used in the development process. Write them out and share them with you. Due to lack of experience, you are welcome to correct any mistakes.

1. Deliver through window. location. href or document. location. href, such as window. location. href= "http://www.ewcar.net? name = sun & age=21 "; In this example, the parameters are name and age, sun and 21, respectively, and of course, the anchor point # can be added, which can be specified to a certain location on the page.

2. Transfer data through Ajax in the following format:


$.ajax{
  type: "post",
  url: "test/index",
  dataType: json,
  data: $("#orderForm").serialize() ,
  success: function(d){
   alert(d.msg);
  }
  error:function(d){
   alert(d.error);
  }
 }

Where data represents the data to be transmitted

3. Pass through the post method of Ajax in the following format:


 $.post({
  "url", 
  $("#orderForm").serialize(),
  function(d){
   alert(d.msg);
  }
 });

Theoretically, this also belongs to the Ajax method, but this method is relatively simple.

4. The action method in the form table can also transmit data, but the action method is relatively more comprehensive, and can jump to connection, text, pictures, videos and other multimedia contents. The links in action are the same as the methods carrying parameters in window. location.

These are the methods I used to pass parameters. If I encounter new methods in the future, I will update them again.


Related articles: