Home > Web Front-end > JS Tutorial > Comparison of several different methods of passing parameters in JS

Comparison of several different methods of passing parameters in JS

高洛峰
Release: 2017-02-16 17:05:09
Original
1183 people have browsed it

This article mainly introduces several different methods of passing parameters in JS. It has a certain reference value, let’s take a look with the editor below

When developing web pages, interacting with front-end and back-end data is an unavoidable problem. Different business needs have different delivery methods. , the following are several delivery methods I have used during the development process. I will write them down and share them with you. Due to lack of experience, you are welcome to correct me if there are any mistakes.

1. Pass through window.location.href or document.location.href, For example, window.location.href="http://www.ewcar.net?name=sun&age =21"; In this example, the parameters carried are name and age, which are sun and 21 respectively. Of course, you can also add the anchor point #, which can be specified to a certain position on the page.

2. Data is transmitted through Ajax. The format is as follows:

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

where data means to be transmitted The data

3. Pass it through the Ajax post method. The format is as follows:

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

Theoretically speaking, this It is also an Ajax method, but this method is relatively simple.

4. The action method in the form can also transfer data, but the action method transfers relatively more comprehensive things. It can jump to connections, text, pictures, videos, etc. Multimedia content, links in actions and window.location carry parameters in the same way.

The above are the parameter passing methods I have used. If I encounter new methods in the future, I will update them.

For more related articles comparing several different methods of passing parameters in JS, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template