亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長(zhǎng)資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    實(shí)現(xiàn)詳解Ajax+php數(shù)據(jù)交互并且局部刷新頁(yè)面

    實(shí)現(xiàn)詳解Ajax+php數(shù)據(jù)交互并且局部刷新頁(yè)面

    什么是Ajax?

    國(guó)內(nèi)翻譯常為“阿賈克斯”和阿賈克斯足球隊(duì)同音,AJAX 是一種用于創(chuàng)建快速動(dòng)態(tài)網(wǎng)頁(yè)的技術(shù),他不是新語(yǔ)言,而是一種使用現(xiàn)有標(biāo)準(zhǔn)的新方法。通過(guò)在后臺(tái)與服務(wù)器進(jìn)行少量數(shù)據(jù)交換,AJAX 可以使網(wǎng)頁(yè)實(shí)現(xiàn)異步更新,這樣就可以在不重新加載整個(gè)網(wǎng)頁(yè)的情況下,對(duì)網(wǎng)頁(yè)的某部分進(jìn)行更新。

    XMLHttpRequest 是 AJAX 的基礎(chǔ),用于和服務(wù)器交換數(shù)據(jù)。所有現(xiàn)代瀏覽器均支持 XMLHttpRequest 對(duì)象(IE5 和 IE6 使用 ActiveXObject)

    下面這篇文章主要介紹的是利用ajax實(shí)現(xiàn)與php數(shù)據(jù)交互,并局部刷新頁(yè)面的相關(guān)內(nèi)容,下面話(huà)不多說(shuō),來(lái)一起看看詳細(xì)的介紹:

    一、語(yǔ)法簡(jiǎn)介

    1.1 ajax基本語(yǔ)法

    $.ajax({  type: "post",        //數(shù)據(jù)提交方式(post/get)  url: "http://xxx/test/demo.php",   //提交到的url  data: {username:username,password:password},//提交的數(shù)據(jù)  dataType: "json",       //返回的數(shù)據(jù)類(lèi)型格式    success: function(msg){   ...//返回成功的回調(diào)函數(shù)  },    error:function(msg){   ...//返回失敗的回調(diào)函數(shù)  } });

    1.2 php端的接收方法

    <!--?php  $username=$_POST['username']; //接收以post方式提交來(lái)的username數(shù)據(jù)  $password=$_POST['password']; ?>

    二、示例詳解

    2.1 html端代碼demo.html

    <!doctype html> <html lang="en"> <head>  <meta charset="UTF-8">  <title>ajaxTest</title> </head> <body>  <input type="text" id="username">  <input type="text" id="password">  <button id="sub">查詢(xún)</button>  <span id="text"></span><!-- 用以顯示返回來(lái)的數(shù)據(jù),只刷新這部分地方 --> </body> <script src="//cdn.bootcss.com/jquery/3.0.0-alpha1/jquery.min.js"></script> </html>

    2.2 在demo.html中加入以下js代碼

    <script>  $(function(){  $('#sub').click(function(){   var username=$('#username').val();   var password=$('#password').val();   $.ajax({   type: "post",   url: "http://xxx/test/demo.php",   data: {username:username,password:password}, //提交到demo.php的數(shù)據(jù)   dataType: "json", //回調(diào)函數(shù)接收數(shù)據(jù)的數(shù)據(jù)格式    success: function(msg){    $('#text').empty(); //清空Text里面的所有內(nèi)容    var data='';    if(msg!=''){    data = eval("("+msg+")"); //將返回的json數(shù)據(jù)進(jìn)行解析,并賦給data    }    $('#text').html("用戶(hù)名為:" + data.username + ",密碼為:" + data.password); //在#text中輸出    console.log(data); //控制臺(tái)輸出   },    error:function(msg){    console.log(msg);   }   });  });  }) </script>

    2.3 php端代碼demo.php

    <!--?php  header('Content-type:text/json;charset=utf-8');  $username=$_POST['username'];  $password=$_POST['password'];    $data='{username:"' . $username . '",password:"' . $password .'"}';//組合成json格式數(shù)據(jù)  echo json_encode($data);//輸出json數(shù)據(jù) ?>

    三、最終效果如下

    實(shí)現(xiàn)詳解Ajax+php數(shù)據(jù)交互并且局部刷新頁(yè)面

    demo下載地址

    下載demo

    相關(guān)學(xué)習(xí)推薦:PHP編程從入門(mén)到精通

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)