国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > AJAX > 正文

Ajax 傳遞JSON實例代碼

2024-09-01 08:27:25
字體:
來源:轉載
供稿:網友

前面的話

  雖然ajax全稱是asynchronous javascript and XML。但目前使用ajax技術時,傳遞JSON已經成為事實上的標準。因為相較于XML而言,JSON簡單且方便。本文將上一篇中的實例進行改寫,以JSON的方式來進行數據傳遞

前端頁面

<!-- 前端頁面 --><!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Document</title><style>body{font-size: 20px;margin: 0;line-height: 1.5;}select,button,input{font-size: 20px;line-height: 1.5;}</style></head><body><h2>員工查詢</h2>  <label>請輸入員工編號:</label><input type="text" id="keyword"><button id="search">查詢</button><p id="searchResult"></p><h2>員工創建</h2><form id="postForm">  <label>請輸入員工姓名:</label>  <input type="text" name="name"><br>  <label>請輸入員工編號:</label>  <input type="text" name="number"><br>  <label>請輸入員工性別:</label>  <select name="sex">  <option value="男">男</option>  <option value="女">女</option>  </select><br>  <label>請輸入員工職位:</label>  <input type="text" name="job"><br>  <button id="save" type="button">保存</button>  </form><p id="createResult"></p><script>/*get*///查詢var oSearch = document.getElementById('search');//get方式添加數據function addURLParam(url,name,value){  url += (url.indexOf("?") == -1 ? "?" : "&");  url +=encodeURIComponent(name) + "=" + encodeURIComponent(value);  return url;}oSearch.onclick = function(){  //創建xhr對象  var xhr;  if(window.XMLHttpRequest){    xhr = new XMLHttpRequest();  }else{    xhr = new ActiveXObject('Microsoft.XMLHTTP');  }  //異步接受響應  xhr.onreadystatechange = function(){    if(xhr.readyState == 4){      if(xhr.status == 200){        //實際操作        var data = JSON.parse(xhr.responseText);        if(data.success){          document.getElementById('searchResult').innerHTML = data.msg;        }else{          document.getElementById('searchResult').innerHTML = '出現錯誤:' +data.msg;        }      }else{        alert('發生錯誤:' + xhr.status);      }    }  }  //發送請求  var url = 'service.php';  url = addURLParam(url,'number',document.getElementById('keyword').value);  xhr.open('get',url,true);  xhr.send();}/*post*///創建var oSave = document.getElementById('save');//post方式添加數據function serialize(form){      var parts = [],field = null,i,len,j,optLen,option,optValue;  for (i=0, len=form.elements.length; i < len; i++){    field = form.elements[i];    switch(field.type){      case "select-one":      case "select-multiple":        if (field.name.length){          for (j=0, optLen = field.options.length; j < optLen; j++){            option = field.options[j];            if (option.selected){              optValue = "";              if (option.hasAttribute){                optValue = (option.hasAttribute("value") ? option.value : option.text);              } else {                optValue = (option.attributes["value"].specified ? option.value : option.text);              }              parts.push(encodeURIComponent(field.name) + "=" + encodeURIComponent(optValue));            }          }        }        break;             case undefined:   //fieldset      case "file":    //file input      case "submit":   //submit button      case "reset":    //reset button      case "button":   //custom button        break;              case "radio":    //radio button      case "checkbox":  //checkbox        if (!field.checked){          break;        }        /* falls through */      default:        //don't include form fields without names        if (field.name.length){          parts.push(encodeURIComponent(field.name) + "=" + encodeURIComponent(field.value));        }    }  }      return parts.join("&");}oSave.onclick = function(){  //創建xhr對象  var xhr;  if(window.XMLHttpRequest){    xhr = new XMLHttpRequest();  }else{    xhr = new ActiveXObject('Microsoft.XMLHTTP');  }  //異步接受響應  xhr.onreadystatechange = function(){    if(xhr.readyState == 4){      if(xhr.status == 200){        //實際操作        var data = JSON.parse(xhr.responseText);        if(data.success){         document.getElementById('createResult').innerHTML = data.msg;        }else{         document.getElementById('createResult').innerHTML = '出現錯誤:'+data.msg;       }      }else{        alert('發生錯誤:' + xhr.status);      }    }  }  //發送請求  xhr.open('post','service.php',true);  xhr.setRequestHeader("content-type","application/x-www-form-urlencoded");  xhr.send(serialize(document.getElementById('postForm')));}</script></body></html>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 竹溪县| 南部县| 景谷| 简阳市| 搜索| 赞皇县| 凤凰县| 青州市| 武鸣县| 义乌市| 茂名市| 同江市| 黄冈市| 孟村| 普陀区| 洪江市| 四会市| 安丘市| 政和县| 德惠市| 教育| 精河县| 昭通市| 新泰市| 潞城市| 建水县| 兖州市| 永靖县| 浦城县| 新邵县| 景东| 应用必备| 淄博市| 邢台县| 隆尧县| 额尔古纳市| 西青区| 沙湾县| 建阳市| 韩城市| 信丰县|