In the previous jquery study, we used jquery and prototype integration to enable jquery tabs to implement local data. Now we use the prototype Ajax method to implement the same functions using jquery, as shown below:
Jquery. JS: http://dl.javaeye.com/topics/download/f961ee8e-24fb-32b5-830a-b18e471e42f9
Jquery is a very powerful class library. I learned about it today and fell in love with it. Let's talk about how to use Ajax in jquery.
Example:
Test.html
Page reference <SCRIPT type = "text/JavaScript" src = "jquery. js"> </SCRIPT>
Content:
<Div id = "my400800"> Hello world! </Div>
Usage 1: (the remote page content is read to divmsg when the page is loaded)
$ ("# My400800"). Load (http://www.my400800.cn, {"resulttype": "html "});
The returned type resulttype is as follows:
"XML", "html", "script", "JSON", "jsonp", "text"
Usage 2: (Click post to return data)
<Input type = "button" id = "bnajax" value = "ajax" onclick = "ajaxtest ()"/>
<SCRIPT type = "text/JavaScript">
Function ajaxtest ()
{
$. Post ("http://www.my400800.cn", {"searchkey": "400 phone"}, function (data)
{
$ ("# My400800" ).html (data );
}
);
}
</SCRIPT>
The following are functions taken from the network:
The post method is as follows:
Function Test (access_url, tipe ){
$. Post (access_url ,{
First: "test1", second: "Test2"
}, Function (data ){
If (data. Success ){
$ ('#' + Tipe).html ('processed successfully ');
} Else {
$ ('#' + Tipe).html (data. msg );
}
}, 'Json'
)
}
If you want to use the get method, you can replace post with get, which is quite simple!
The data value in this function is the value returned by the server in JSON format. Of course, other types such as text and XML can be used here.
The return value on the server is in JSON format, for example, {success: True, MSG: "tested "}
Ajax garbled
Cause of garbled characters:
1. The default character encoding of data returned by xtmlhttp is UTF-8. If the client page is gb2312 or other encoding data, garbled characters are generated.
2. The default character encoding for data submitted by the POST method is UTF-8. If the server side is gb2312 or other encoding data, garbled characters are generated.
Solutions:
1. If the client is gb2312 encoded, specify the output stream encoding on the server.
2. Both the server and client use UTF-8 encoding.
Gb2312: Header ('content-type: text/html; charset = gb2312 ');
Utf8: Header ('content-type: text/html; charset = UTF-8 ');
Note:If you have already followed the above method and returned garbled characters, check whether your method is get. For GET requests (or parameters involved in URL transfer), the transmitted Parameters
You must first use the encodeuricomponent method. If you do not use encodeuricomponent for processing, garbled code will also be generated.