Front desk Ajax needs to be aware of the place

Source: Internet
Author: User

0x01.

Using $.post ()/$.get ()/$.ajax () in jquery ()/$.load () This is a common four ways to interact with the background of Ajax

General idea:

The front-end JS Trigger event invokes the jquery function, submits the parameters to the background, processes the data in the background, returns the data result set (JSON format), and the information assigned by the front desk transfer to the foreground page display.

1. Foreground events

Click: Mouse click event Application Range: button, form submit ...

onchange: drop-down list value change <select><option value= "" ></select>

...

Calls to 2.js functions

...

$ (function () {

$ ("button"). Click (function () {

var data= $ ("#form"). Val (); The data received may be one or more

$.load ("url", "Data", function (response,status) {

if (status== "success") {

#处理json数据 assignment to an HTML page

}

if (status== "error") {

#错误信息

}

});

})

})

If this is the Change event in <select>

Modified to $ (function () {

$ ("#select"). Change (function () {

#....

})

})

3. Back end returns JSON data

Public Function Ajax () {

.....

$data ["id"]= "";

$data ["Name"]= "";

$this->ajaxreturn ($data); The method in thinkphp is called directly here

}

Data format:

Single data: {"id": "1", "name": "XXX"}

Multiple array data: {"0": ["id": "1", "Name": "XX"], "1": ["id": "2", "name": "XXX"],...,}

Sometimes with status information: {"0": ["id": "1", "Name": "XX"], "1": ["id": "2", "name": "XXX"],..., "refer": "", "State": "}

0x02.

Processing JSON data traversal

...

Front desk Ajax needs to be aware of the place

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.