• ajax全接触


    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    </head>
    <body>
    编号:
    <input type="text" id="keyword">
    <button id="search">查询</button>
    <br/>
    <form>
    <label>姓名:</label>
    <input id="staffName" type="text" />
    <label>职位:</label>
    <input type="text" id="staffJob" />
    <input type="button" id="searchResult" value="保存">
    </form>
    <script type="text/javascript">
    //get
    document.getElementById('search').onclick=function(){
    //发送Ajax查询请求并处理
    var request = new XMLHttpRequest();
    request.open('GET','service.php?number+document.getElementById("keyword").value');
    request.send();
    request.onreadystatechange=function(){
    if(request.readyState ===4){
    if(request.status ===200){
    document.getElementById('searchResult').innerHTML =request.responseText;
    }else{
    alert('发生错误:'+request.status);
    }

    }
    }
    }
    //post

    document.getElementById('save').onclick=function(){
    //发送Ajax查询请求并处理
    var request = new XMLHttpRequest();
    request.open('POST','service.php');
    var data="name="+document.getElementById("staffName").value+"job="+document.getElementById("staffJob").value;
    request.setRequestHeader('Content-Type',"application/x-www-form-urlencoded");
    request.send(data);
    request.onreadystatechange=function(){
    if(request.readyState ===4){
    if(request.status ===200){
    document.getElementById('createResult').innerHTML =request.responseText;
    }else{
    alert('发生错误:'+request.status);
    }

    }
    }
    }
    </script>

    <!-- json -->
    {
    "staff":[
    {"name":"红旗","age":70},
    {"name":"郭靖","age":35}
    ]
    }
    <!--
    eval()
    JSON.parse()
    -->

    <!-- 服務器端application/json; -->

    <script type="text/javascript">
    //get
    document.getElementById('search').onclick=function(){
    //发送Ajax查询请求并处理
    var request = new XMLHttpRequest();
    request.open('GET','service.php?number+document.getElementById("keyword").value');
    request.send();
    request.onreadystatechange=function(){
    if(request.readyState ===4){
    if(request.status ===200){
    var data =JSON.parse(request.responseText);
    if(data.success){
    document.getElementById('searchResult').innerHTML =data.msg;
    }else{
    document.getElementById('searchResult').innerHTML ="出現錯誤"+data.msg;
    }

    }else{
    alert('发生错误:'+request.status);
    }

    }
    }
    }
    //post

    document.getElementById('save').onclick=function(){
    //发送Ajax查询请求并处理
    var request = new XMLHttpRequest();
    request.open('POST','service.php');
    var data="name="+document.getElementById("staffName").value+"job="+document.getElementById("staffJob").value;
    request.setRequestHeader('Content-Type',"application/x-www-form-urlencoded");
    request.send(data);
    request.onreadystatechange=function(){
    if(request.readyState ===4){
    if(request.status ===200){
    var data =JSON.parse(request.responseText);
    if(data.success){
    document.getElementById('creatResult').innerHTML =data.msg;
    }else{
    document.getElementById('creatResult').innerHTML ="出現錯誤"+data.msg;
    }
    }else{
    alert('发生错误:'+request.status);
    }

    }
    }
    }
    </script>

    <!-- jquery實現Ajax -->
    type:post get
    url:
    data:
    dataType
    success
    error


    <script type="text/javascript">
    //发送Ajax查询请求并处理 jquery
    //先引用jquery庫
    //
    $(document).ready(function(){
    //get
    $("#search").click(function () {
    $.ajax({
    type:"GET",
    url:"service.php?number="+$("#keyword").val(),
    dataType:"json",

    //dataType:"jsonp",

    //jsonp:"callback",//callback随便取名字
    success:function(){
    if(data.success){
    $('#searchResult').html(data.msg);
    }else{
    $('#searchResult').html("出現錯誤"+data.msg);
    }
    },
    error:function(jqXHR){
    alert('发生错误:'+jqXHR.status);
    }
    })
    })

    //post
    $("#save").click(function () {
    $.ajax({
    type:"POST",
    url:"service.php",
    dataType:"json",
    data:{
    name:$('#searchResult').val(),
    job:$('#searchJob').val(),
    }
    success:function(){
    if(data.success){
    $('#searchResult').html(data.msg);
    }else{
    $('#searchResult').html("出現錯誤"+data.msg);
    }
    },
    error:function(jqXHR){
    alert('发生错误:'+jqXHR.status);
    }
    })
    })
    });

     

    </script>
    </body>
    </html>

  • 相关阅读:
    C#读取资源文件的两种方法及保存资源文件到本地
    T4模板编辑器
    VS (Visual Studio) 快捷键
    以太网帧类型速查
    线程间操作无效: 从不是创建控件“XXX”的线程访问它
    C# 集合
    C#控件之ListView
    C# Color颜色对照表
    Java的内存需要划分成为5个部分:
    Java学习:数组的使用和注意事项
  • 原文地址:https://www.cnblogs.com/zhouyx/p/7008034.html
Copyright © 2020-2023  润新知