Xem điểm thi Đại Học 2010 miễn phí tại Học vui
Advanced Search

New Reply

Author Topic: Ajax post method  (Read 488 times)

0 Members and 1 Guest are viewing this topic.

Ajax post method
« on: March 13, 2010, 09:57:06 AM »

chắc nhiều bạn không hề lạ lầm gì với cách dùng ajax và phương thức get để gửi thông tin tới file xử lý, trong một số trường hợp cách thức này sẽ không hay, cũng như lý do tại sao người ta hay dùng phương thức post hơn là get cho form vậy. Bài viết này xin giúp bạn một đoạn code bày cách gửi request ở dạng post.
Code: [Select]
function HocvuiAjaxFunction()
{
var xmlhttp;
var params = "site=hocvui.net website";
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4){
if (xmlhttp.status==200)
//mã cần thực hiện khi nhận được tín hiệu trả về
alert(xmlhttp.responseText);
else
alert('Error!!!');
}
}
xmlhttp.open("POST", "http://localhost/ajax2.php", true);
//Send the proper header information along with the request
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-length", params.length);
xmlhttp.setRequestHeader("Connection", "close");
xmlhttp.send(params);
}
window.onload = HocvuiAjaxFunction();
tahy giá trị của biến params với thông tin cần gửi.
các bạn thay dòng http://localhost/ajax2.php bằng link dẫn tới file xử lý.
Trong ví dụ trên biến params có giá trị là "site=hocvui.net website" thì trong file xử lý ta cho một đoạn mã in ra giá trị của biến site được lưu trong mảng post. VD với php:
Code: [Select]
echo $_POST['site'];
test thử nhé!


bookmark_facebook    bookmark_google    bookmark_twitter    bookmark_yahoo    bookmark_baihay    bookmark_linkhay    bookmark_tagvn    bookmark_zingme

« Last Edit: March 13, 2010, 10:20:19 AM by nvcnvn »

New Reply
 

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Name: Email:
Verification:
Một với 1 là mấy!? (trả lời = chử, VD: chín):

Thích hợp với các trình dyệt:

Firefox - Opera- Google Chrome

Diễn đàn Lê Thánh Tôn

Dạy học trực Tuyến Bacgiang gifted high school 4upro.info Driver4Vn