Ajax 解释Ajax的XML和JSON格式( 二 )


这里说一下eval函数,意思评估 。
eval() 函数使用的是编译器,可解析 JSON 文本,然后生成对象 。必须把文本包围在括号中,这样才能避免语法错误:
var obj = eval ("(" + txt + ")");
在网页中使用对象:
var txt = '{ "sites" : [' +'{ "name":"菜鸟教程" , "url":"www.djznrobot.com" },' +'{ "name":"google" , "url":"www.djznrobot.com" },' +'{ "name":"微博" , "url":"www.djznrobot.com" } ]}';var obj = eval ("(" + txt + ")");document.getElementById("name").innerHTML=obj.sites[0].namedocument.getElementById("url").innerHTML=obj.sites[0].url
3.如何处理XML数据格式案例
注意:在php文件中,xml代码和php代码中间一定不要有多余的换行,否则不能执行!!!
写一个案例,创建一个.html文件:
书籍列表案例type="text/css">div{width: 800px;margin: 20px auto;}table{width: 800px;margin: 20px auto;border-collapse: collapse;}th{background-color: #0094ff;color: white;font-size: 16px;padding: 5px;text-align: center;border: 1px solid black;}td{padding: 5px;text-align: center;border: 1px solid black;}type="text/javascript">window.onload = function(){var xhr = new XMLHttpRequest();xhr.open("get","./server/getBooks.php",true);xhr.send(null);xhr.onreadystatechange = function(){if (xhr.readyState == 4) {if (xhr.status == 200) {var result = xhr.responseXML;var books = result.getElementsByTagName("booklist")[0].getElementsByTagName("book");var newHtml = document.getElementById("bookContaintor").innerHTML;for(var i=0;i

书名作者描述

再在下创建一个.php文件:
Ajax 解释Ajax的XML和JSON格式

文章插图
type="text/javascript">window.onload = function(){var username = document.querySelector("#username");var email = document.querySelector("#email");var phone = document.querySelector("#phone");username.onblur = function(){var usernameValue = http://www.kingceram.com/post/username.value;var type ="get";var url = "./server/checkUsername.php";var params = "uname="+usernameValue;var dateType = "text";myAjax2({url:url,data:{uname:usernameValue},dataType:"text",success:function(result){var username_result = document.querySelector("#username_result");if (result == "NO") {username_result.innerText = "user已经注册!!";}else{username_result.innerText = "user可以使用 。";}}});}email.onblur = function(){var emailValue = http://www.kingceram.com/post/email.value;var type ="post";var url = "./server/checkEmail.php";var params = "e="+ emailValue;var dataType = "text";myAjax2({type:type,url:url,data:{e:emailValue},dataType:"text",success:function(){var email_result = document.querySelector("#email_result");if (result==0) {email_result.innerText = "邮箱可用";}else{email_result.innerText = "邮箱不可用";}}});}phone.onblur = function(){var phoneValue = http://www.kingceram.com/post/phone.value;var type ="post";var url = "./server/checkPhone.php";var params = "phonenumber="+phoneValue;var dataType = "json";myAjax2({type:type,url:url,data:{phonenumber:phoneValue},dataType:"json",success:function(){var phone_result = document.querySelector("#phone_result")if (result.status == 0 ) {//代表手机号码可用phone_result.innerText = result.message.tips+","+result.message.phonefrom;}else if (result.status == 1) {//代表手机号码不可用phone_result.innerText = result.message;}}});}}注册界面
用户名: