The interaction between thinkphp and Ajax ____php

Source: Internet
Author: User

the interaction between thinkphp and Ajax


Recently learning PHP, in the learning process encountered some problems, one of the problems is thinkphp and Ajax data exchange between.

The main function of Ajax is that Ajax enables asynchronous updating of Web pages by making a small amount of data exchange in the background with the server. That is to implement data or form submission without refreshing the page.

1. Basic use of Ajax

To use the Get method:
 	$.ajax ({
                        type: "Get",
                        URL: "__url__/deleteuser",
                        data:{id:ids},
                        DataType: "JSON",
                        success: Function (ss) {
                            alert (ss);
                        }
                    });
To use the Post method:
FormData () is the data passed
var form = new FormData ();
Form.append ("id", id);
$.ajax ({                        type: "POST",     &N Bsp                   URL: "__url__/deleteuser",         &NBSP ;                Data:form,                         Processdata:false,  //not processing data   Contenttype:false,                         Success:function (ss) {             
          alert (ss);                            //Window.history.go (-1)
;                        },                         error:function () {      &NBsp
                alert ("error");                                   & nbsp
        });

Parameter resolution:

Type: Represents the method of the request, post or get, default to get.

URL: The URL address to which the data is to be submitted.

Data: Represents the information to be submitted. JSON format (key-value pair). The first ID in the code represents the variable name, and the second IDs represents the specific value, which is the corresponding value of the first ID variable.

DataType: Expected data type returned by the server. If not specified, jquery will automatically be based on the HTTP packet MIME information to intelligently judge (the copy of the WWW, specifically do not understand, presumably according to the type of mime to return the type of value)

Success: The callback function that is invoked after the request succeeds.

Ajax request data There are a lot of parameters, such as Beforesend, error, and so on, if you want to learn more about the Internet to check.

With this code, the user can submit the data to the background in a get way when performing a click operation.

function DeleteUser () {        
        if (is_get)
        {
            $id =$_get["id"];
            $this->ajaxreturn ($id);
        }
	if (is_post)
        //{
          //  $id =$_post["id"];
           $this->ajaxreturn ($id);
        //}
}

In thinkphp, there is an encapsulation of the type of request, which has a judgment on several types of requests:

Is_post: Determine if the POST method is submitted

Is_get: Determine if a Get method commits

Is_ajax: Determine if it is an AJAX way to commit


When the data is received in the background, the request is also judged first. Then you get the data submitted by the page by $id=$_get["id", and then the data is returned.

thinkphp support for Ajax, the system \think\controller class provides a Ajaxreturn method for returning data to the client after an Ajax call, supporting the type of json,jsonp,xml and eval four ways to return data. Returns data by default in JSON format.



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.