1、前端页面default.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> new document </title> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <meta name="generator" content="editplus" /> <meta name="author" content="" /> <meta name="keywords" content="" /> <meta name="description" content="" /> <script src="./qrcodejs/jquery.min.js"></script> <script> $(function () { var d={}; var t = $('form').serializeArray(); $.each(t, function() { d[this.name] = this.value; }); //console.log(JSON.stringify(d)); $("#submitAB").click(function () { $.ajax({ url: "http://localhost/download.php", type: "POST", contentType: "application/json; charset=utf-8", datatype: "JSON", //data: JSON.stringify({parameter_A:1,parameter_B:'liuchao'}), data: JSON.stringify(d), success: function (data) { var json2 = JSON.parse(data); console.log(json2.name); //dingxiaobo... }, error: function () { alert("提交失败!"); } }); }); }); </script> </head> <body> <input id="submitAB" type="button" value="提交" /> <form> <input id="parameter_A" name="parameter_A" type="text" value="1" /> <input id="parameter_B" name="parameter_B" type="text" value="dingxiaobo..." /> </form> </body> </html>
2、后台download.php
<?php $ret = array( 'name'=>$params['parameter_B'], 'age'=>34, ); echo json_encode($ret);