欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Ajax提交表單并接收json實(shí)例代碼

 更新時間:2017年06月26日 08:37:00   作者:未竟之夢  
這篇文章主要介紹了Ajax提交表單并接收json的實(shí)例代碼,非常不錯,具有參考借鑒價(jià)值,需要的朋友可以參考下

需求:

實(shí)現(xiàn)點(diǎn)擊按鈕后,數(shù)據(jù)以表單形式提交至服務(wù)器,并接收來自服務(wù)器的返回?cái)?shù)據(jù)。過程中頁面不刷新。

html代碼

<html xmlns="http://www.w3.org/1999/xhtml">
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
 <script src="./testajaxjs.js"></script>
 <head>
 </head>
 <body>
  <form id="form1">
   <p>xingming:<input type="text" name="xingming"/></p>
   <p>nianling:<input type="text" name="nianling"/></p>
  </form>
  <button type="button" id="mybt" onclick="mysubmmit()">
   ajax提交
  </button>
 </body>
</html>

js代碼

function mysubmmit(){
 $.ajax({
  type: "POST",
  url: "testajaxend.php",
  data: $('#form1').serialize(),
  async: false,
  success: function(databack){
   //console.log("chenggong");
   console.log(databack);
  },
  error: function(request){
   console.log("shibaile");
  }
 });
}

后端php代碼

<?php
  $name = $_POST['xingming'];
  $age = $_POST['nianling'];
  $myarray = array("name"=>$name, "age"=>$age);
  $myjson = json_encode($myarray);
  echo $myjson;
?>

以上所述是小編給大家介紹的Ajax提交表單并接收json實(shí)例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論