JQuery $.post $.ajax Usage

Source: Internet
Author: User

Jquery.post (URL, [data], [callback], [type]): Use post to make asynchronous requests

Parameters:

URL (String): The URL address of the sending request.

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

Callback (function): (optional) The callback function when loading succeeds (the method is called only if the return state of response is success).

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

This is a simple POST request function to replace the complex $.ajax. The callback function can be called when the request succeeds. If you need to execute the function on error, use $.ajax. Example code:

Ajax.aspx:

response.contenttype =  application/ Json "; Response.Write ( "{result: " " + request[ "name "] +  " Hello! (This message comes from the server) '} ' ';

JQuery Code:

$.post ("Ajax.aspx", {Action:"Post", Name:"Lulu"},
function(Data, Textstatus) {
data can be xmldoc, jsonobj, HTML, text, and so on.
//This;
//// The option configuration information for this AJAX request, please refer to the Jquery.get () mentioned in this
alert (Data.result); }, "json");

Click Submit:

The format of the request is "JSON":


$.ajax () This is the underlying AJAX implementation of jquery. Easy to use high-level implementation see $.get, $.post and so on.

Here are a few Ajax event parameters:beforesend ,success , complete, error. We can define these events to handle each of our AJAX requests well.

$.ajax ({
Url:‘stat.php‘,

Type‘POST "
Data:{name: "keyun" },

Datatype:  ' html '
timeout:  1000
error:  function () {alert (error loading php document '
Success: function
});
/span>

Add by Q at 2008.11.25

A little problem with the post of jquery today

Because you want to delete it in bulk, start by looping the post to that URL, and then refresh this page

There's a problem here.

$("input[@name = ' Qa_checkbox ')"). each (function()
{
If($(This). attr (‘Checked‘)== undefined)
    {
                 
    }
     else
    {
         $.post (Url,{action: " post},function "text" );
                
     }
})


You can only delete the first piece of data if you use it.

$("input[@name = ' Qa_checkbox ')"). each (function()
{
If($(This). attr (‘Checked‘)==Undefined
{

}
Else
{
$.post (URL+$ (this). Val (), {Action:"post"},function(data) {alert (data);} , "text");

}
})

Window.location.reload ();

In this case, although it can be deleted, you can also refresh this page, it seems that reload is in the post function before the operation, but post will be error, the reason for study;

Finally thought of a compromise.

$("input[@name = ' Qa_checkbox ')"). each (function()
{
If($(This). attr (‘Checked‘)==Undefined
{

}
Else
{
Url=Url+$(This). Val ()+  _ '

String the ID to be deleted, use a post processing, put the reload in the function of the post there is no problem

Project Examples:

Function Clearsys () {
    $.dialog.confirm (' Do you confirm the delete operation? ', function () {
        $.post ("{: GetUrl (' Ucentermsg/clearsys ')}", function (data) {
        if (!data) {
             Kshop.optfail ();
            return false;
        }
        if (data.status = = 1) {
             settimeout (' window.location.href=window.location.href; ', 1000);
            kshop.optdone (data.info);    
        }else{
             Kshop.optfail (Data.info);
        }    
    }, ' json ');
    });
}

Background:

Public Function Clearsys ()
{
$msg = D (' Message ');
$result = $msg->saveallflags ($this->user_info[' uid '), $msg:: Sys_to_uid);
if ($result) {
$this->ajaxreturn (', ' Delete succeeded! ', 1);
} else {
$this->ajaxreturn (', ' Delete failed! ', 0);
}
}

protected function Ajaxreturn ($data, $info = ', $status =1, $type = ') {
$result = Array ();
$result [' status '] = $status;
$result [' info '] = $info;
$result [' data '] = $data;
Extend the Ajax return data, define the function Ajaxassign (& $result) {} method in action to extend the Ajax return data.
if (Method_exists ($this, "ajaxassign"))
$this->ajaxassign ($result);
if (empty ($type)) $type = C (' Default_ajax_return ');
if (Strtoupper ($type) = = ' JSON ') {
Returns the JSON data format to the client that contains state information
Header ("content-type:text/html; Charset=utf-8 ");
Exit (Json_encode ($result, Json_hex_tag));
}elseif (Strtoupper ($type) = = ' XML ') {
return XML Format data
Header ("Content-type:text/xml; Charset=utf-8 ");
Exit (Xml_encode ($result));
}elseif (Strtoupper ($type) = = ' EVAL ') {
Returns the executable JS script
Header ("content-type:text/html; Charset=utf-8 ");
Exit ($DATA);
}else{
TODO Add other formats
}
}

JQuery $.post $.ajax Usage

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.