Comparison of several methods for passing parameters in JS

Source: Internet
Author: User
This article mainly introduces several different methods for passing parameters in JS. It has some reference value. Let's take a look at it below. This article mainly introduces several different methods for passing parameters in JS. It has some reference value. Let's take a look at it together with the small Editor.

During webpage development, interaction with the front-end and back-end data is an unavoidable problem. Different business needs have different transmission methods, below are several transfer methods I have used in the development process. I will write them out and share them with you. Due to lack of experience, you are welcome to correct the errors.

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 are name and age, respectively sun and 21. Of course, you can also add the anchor #, you can specify a location on the page.

2. Use Ajax to transmit data 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);    }  }

Data indicates the data to be transmitted.

3. The post method of Ajax is used for transmission. The format is as follows:

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

In theory, this is also an 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. You can jump to the connection or to text, images, videos, and other multimedia content, the link in the action and window. location carries parameters in the same way.

The above is the parameter passing method I have used. If you encounter a new method, update it again.

For more articles about how to compare different methods for passing parameters in JS, please refer to PHP!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.