Sinsing An analysis of the implementation process of Ajax

Source: Internet
Author: User
Tags jquery library

when it comes to Ajax, it's definitely a cliché, and the use of AJAX technology has been rife in recent years. Let's take jquery as an example to look at an example of Ajax from a real project.

The first is the front page, this page we are using bootstrap and some of their own technology, finally we have a button, its source code is as follows:

$deal. = ' <a href= ' javascript:void (0); "class=" btn btn-danger btn-mini "onclick=" Deleteone ('. $Id. '); " > Delete </a> ';

I believe that there are JS basic friends to read this code is not a problem, here are a few things to explain:

1. It uses bootstrap, so we'll see things like btn Btn-danger Btn-mini,

2. It calls a Deleteone () function, and the parameter $id in the function is a variable in PHP.


Then there is the code for this Deleteone method, note that here we import the jquery library, and of course some other libraries, where only the more important part of the code is written:

<script type= "Text/javascript" >var base_url= ' <?php echo base_url ();? > '; $ (function () {deleteone=function (id,time) {var t= ' n '), if (IsNullOrEmpty (time)) {t= ' 3000 ';} Else{t=time;} var icon= ' <?php Echo base_url ();? >assets/dialog/icons/'; ShowDialog (' OK to delete? ', function () {var url= ' <[email protected] $site _url. ' /' [email protected] $module. ' /delete ';? > '; var data={' Id ': Id}$.ajax ({//async:true,//is an asynchronous request type: "POST",//get post Url:url,//        Data:data, data: ' id= ' +id,//datatype: ' JSON ',//beforesend:function (XMLHttpRequest, Textstatus) {},        Success:function (data) {//,textstatus//console.log (data);        var msg=data.message; if (data.status) {Refreshgrid (); i= ' Succeed.png ';}        Else{i= ' Info.png ';}        ShowDialog (msg);        Window.location.reload (); }//complete:function (XMLHttpRequest, Textstatus) {},/*error:function (XMLHttpRequest, Textstatus, ERRORTHR Own) {var msg= ("Error "); i= ' error.png ';        ShowDialog (msg); }*/    });});}}); </script>

Here, we are one step ahead of completion, is the Delete method, the following is its implementation process:

Public Function Delete () {if (! IsPost ()) {$msg = ' the link entered is incorrect! '; showerrormsg ($msg);} $action = ' delete '; $module = $this->module; $arrLang =lang (' Common_ '. $module); $title = $arrLang [$action]; $arr _post= $this->input->post (); $arr _post=setform ($arr _post); [Email protected] $arr _post[' id ']; $status =false; $logType =0;if (!isnullorempty ($Id)) {$del _where=array (' id ' = = $Id ); $this->common_model->table= ' mx_changci '; $result = $this->common_model->delete ($del _where); if ($ Result) {$status = TRUE; $message =  $title. ' Success! ';} else {$status = FALSE; $message =  $title. ' Failure! ';}} else{$status =false; $message = $title. ' Error! '; $logType = 3;} $jsonData [' status ']= $status; $jsonData [' Message ']= $message; Header ("Content-type:application/json"); Echo Json_ Encode ($jsonData);}

This is a simple AJAX implementation of a project I've been involved in recently.

Sinsing An analysis of the implementation process of Ajax

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.