jquery中$.ajax的$.get与$.post使用
<script type='text/javascript' src='http://ajax.useso.com/ajax/libs/jquery/1.7.2/jquery.min.js?ver=3.4.2'></script> <script type="text/javascript"> $(function(){ var bd=$("form")[0]; $(bd).submit(function(){ var cc="addr="+$("#uaddr").val()+"&name="+$("#uname").val(); $.post("4.php",cc,function(msg){alert(msg.addr+"----"+msg.name)},'json'); return false; }); }); </script> <form action="4.php"><br> <input name="uname" type="text" id="uname"><br> <input name="uaddr" type="text" id="uaddr"> <input type="submit" value="提交"></form>
<script type='text/javascript' src='http://ajax.useso.com/ajax/libs/jquery/1.7.2/jquery.min.js?ver=3.4.2'></script> <script type="text/javascript"> $(function(){ var bd=$("form")[0]; $(bd).submit(function(){ $.ajax({ url:'4.php', data:{addr:$("#uaddr").val(),name:$("#uname").val()}, dataType:"json", type:'POST', success: function(msg){ //msg是服务器返回信息 alert(msg.addr+"----"+msg.name) } }); return false; }); }); </script> <form action="4.php"><br> <input name="uname" type="text" id="uname"><br> <input name="uaddr" type="text" id="uaddr"> <input type="submit" value="提交"></form>
<?php $arr=array(); $arr["addr"]=$_POST["addr"]; $arr["name"]=$_POST["name"]; echo json_encode($arr); ?>