ajax 从网页端发送数据到php端

来源:互联网 发布:软件测试案例分析实践 编辑:程序博客网 时间:2024/06/07 03:30

ajax的使用需要在导入了 jquery之后才能使用,ajax在使用能将数据从网页端发送到sever端,在使用的时候就跟form表单差不多,但个人感觉要比form表单实用,基本上form能实现的ajax都能实现。

ajax支持很多格式的传送,Jason, 还有数组 text 等,

$.ajax({    url:"/php/ajax/checklogin.php",    type:"POST",    datatype:"text",    data:{"uname" : uname.value,        "upassword" : upassword.value,        "remember" : checked,    },    success:function(data) {        if(data == 0){            alert( "用户名或密码错误");            window.location.reload();            Ext.MessageBox.alert('failure msg', "Delete Error");        }else if(data == 1){            alert ("您没有权限");            window.location.reload();            Ext.MessageBox.alert('failure msg', "Delete Error");        }else {            window.location.reload();        }    }});
上代码是将数据提交到checklogin.php中,使用的是post请求,数据格式是数组的形式,返回的数据都存在data里面,datatype表示的是数组返回时的类型,type表示的是数组发送的请求的类型。


在实现跳转后,在跳转的页面可以对数据进行操作,使用$_POST来获取数据 操作返回直接用echo输出,判断后如果需要页面进行跳转,能直接使用js的页面跳转代码在if-else中来实现所要的页面跳转。


当前页面加载其他文本内容可以用load来实现

txt的内容是

<h2>jQuery and AJAX is FUN!!!</h2><p id="p1">This is some text in a paragraph.</p>

$("#div1").load("demo_test.txt",function(responseTxt,statusTxt,xhr){
      if(statusTxt=="success")
        alert(responseTxt);
      if(statusTxt=="error")
        alert("Error: "+xhr.status+": "+xhr.statusText);
    });

load会将 id 是 div1 的元素里的值用txt 里的东西替换, 在执行结束后会执行function, responseTxt 就是txt里的内容, statusTxt 是返回是否替换成功。

<h2>jQuery and AJAX is FUN!!!</h2><p id="p1">This is some text in a paragraph.</p>




0 0