$.ajax $.post load usage and difference in jquery

Source: Internet
Author: User

$.ajax usage

The code is as follows Copy Code

function Getverify () {
if ($ (' #regRuser '). Val ()) {
var m=$ (' #regRuser '). Val ();
if (/^13d{9}$/g.test (m) | | (/^15[0-35-9]d{8}$/g.test (m)) | | (/^18d{9}$/g.test (m))) {
var postdata = $ (' #reg_form '). Serialize ();
$.ajax ({
Type: "POST",
URL: "/ajax/getverify",
Data:postdata,
Success:function (msg) {
if (msg>0) {
Alert (' Verification code has been sent to your cell phone, please check ');
$ (' #getVerify '). Hide ();
$ ('. Regnav '). CSS (' background ', ' url (/static/images/v10/regnav2.png) ');
$ (' #checkvalidate '). Show ();
F_timeout ();
$ ('. ABC '). Show ();
}else if (msg==0) {
Alert (' Mobile number has been registered ');
}
}
});
}else{
Alert (' Please enter the correct phone number ');
}
}
}

Forestall's friends will find that there is a postdata data in the middle , but by $ (' #reg_form '). Serialize (); and then $ (' #reg_form '). Serialize (); What data will it be, let's take a simple example

$. Form.serialize (Namevaluepair)

Virtual a form, and set the name and value of the form control.

Parameters
Namevaluepair
Required option. Set up a virtual form control. This parameter is in the form of: {name1=value, name2=value2, ...}
return value
The string after the virtual form is serialized, in the form of: username=%e5%95%8a%e8%94%a1&password=123456

Describe
A control with disabled=true or no Name property in the form will be ignored. A control with the same name sends a set of strings with the same name but different values. This method is typically used for the Ajax submission parameters (parameters).

Example

The code is as follows Copy Code

<form method= "POST" Name= "Reg" action= "http://localhost"
  <input type= "text" name= "username"/
  <input type= "password" name= "password"/>
  <input type= "checkbox" name= "Interest" Value= "Music"/>//New User Interest Hobby survey, tick
  <input type= "checkbox" name= "Interest" value= "sport"/>//Tick
& nbsp <input type= "checkbox" name= "interest" value= "Dance"/>//tick
  <input type= "Submit" value= "User Registration"/>
  <input type= "button" value= "Submit form in JavaScript" onclick= "Test ()"/>
</form>
<script Type= "Text/javascript"
Function test () {
     alert ($. Form ("Reg"). Serialize ({regtime:new Date (). togmtstring ())); //serizlize method complements a time control, the name=regtime of the control, and the value is the current time.

//username=%e5%95%8a%e8%94%a1&password=123456&interest=music&interest=sport&interest=dance &REGTIME=THU%2C%2027%20SEP%202007%2008%3A35%3A55%20UTC.
Notice the form in the same name
}
</script>

From the above you can see that this is an AJAX get format, the amount of data can not be too large.


$.post

Jquery.post (URL, [data], [callback], [Type]): Asynchronous request using POST method


Parameters:

URL (String): The URL address where the request is sent.

Data (MAP): (optional) The information to be sent to the server, expressed as a Key/value key-value pair.

Callback (function): (optional) the callback function (which is invoked only if the response return state is success) when loading succeeds.

Type (String): (optional) The official description is: type of data to is sent. The type that should actually be requested for the client (Json,xml, etc.)

. Post ("Ajax.aspx", {Action: "Post", Name: "Lulu"}, function (data, textstatus) {//data can be xmldoc, JS      Onobj, HTML, text, and so on.    This        For this AJAX-requested option configuration information, refer to the This alert (Data.result) as mentioned in Jquery.get (). }, "JSON"; Click Submit:

Instance

The code is as follows Copy Code

function   Sendmsg ()
{
 var isphone=  checkphone ($ ("#username"). Val ());
 if (!isphone)
 {
  $ ("#btn"). Val ("Send in ...");
  $.post ("momsg.php", {"Tel": $ ("#username"). Val ()},function (data) {
    //alert (data);
    if (data==0)//0  success   1  unsuccessful   2 phone number format is not correct
    { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP
     //    
   &NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP
    
  });
 }
 else
 {
  alert (' Please enter the correct phone number! '); $ ("#username"). focus ();
 }
 
}

This most central sentence is $.post ("momsg.php", {"Tel": $ ("#username"). Val ()} , what does this mean, this is Ajax $.post is the same as PHP or submit form Post Previous field name, later variable or numeric value.


$ (' #xxx '). Load (Url,para)


The full format for calling the Load method is: Load (URL, [data], [callback]),

URL: Refers to the address of the file to be imported.
data: Optional parameters; Because load can not only import static HTML files, but also import dynamic scripts, such as PHP files, so to import a dynamic file, we can put the parameters to pass here.
callback: optional parameter; is another function that is executed after the Load method is invoked and the server responds.

The code is as follows Copy Code

$ ("#content"). Load ("Xx.aspx under your own website");

In your xx.aspx use WebRequest access "http://www.baidu.com, return the result to JS

One: How to use Data

1. Load a php file, the PHP file does not contain pass parameters

The code is as follows Copy Code
$ ("#myID"). Load ("test.php");

Import test.php Run results in an element with ID #myid
2. Load a PHP file that contains a pass parameter

The code is as follows Copy Code

$ ("#myID"). Load ("test.php", {"name": "Adam"});

The imported PHP file contains a pass-through parameter, similar to the following: Test.php?name=adam
3. Load a PHP file that contains multiple pass parameters. Note: The parameters are separated by commas

The code is as follows Copy Code

$ ("#myID"). Load ("test.php", {"name": "Adam", "Site": "61dh.com"});

The imported PHP file contains a pass-through parameter, similar to the following: test.php?name=adam&site=61dh.com

4. Load a php file, the php file with an array as the passing parameters

The code is as follows Copy Code

$ ("#myID"). Load ("test.php", {' myinfo[] ', ["Adam", "61dh.com"]});

The imported PHP file contains an array pass parameter.
Note: Using load, these parameters are passed as post, so you cannot use get to obtain parameters in test.php

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.