国产探花免费观看_亚洲丰满少妇自慰呻吟_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>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 鸡东县| 鲁甸县| 洛阳市| 昌都县| 太康县| 闵行区| 廊坊市| 上饶市| 广南县| 贵州省| 商河县| 重庆市| 郧西县| 阳谷县| 神农架林区| 万州区| 岑巩县| 正安县| 安阳县| 鸡东县| 安宁市| 永和县| 沾益县| 简阳市| 蕲春县| 韩城市| 哈尔滨市| 石楼县| 衢州市| 曲水县| 富源县| 康乐县| 新津县| 临汾市| 昌邑市| 濉溪县| 西乌珠穆沁旗| 陆良县| 凉城县| 临清市| 且末县|