ajax详细用带返回值
发布时间:2020-12-16 01:53:39 所属栏目:百科 来源:网络整理
导读:先引入jquery.js 网页前台JS代码 script type="text/javascript"function getInfo() {//var id = document.getElementById('goodsid').value;//alert(id);$.ajax({url : 'ajax2.php',type : 'post',data : {'id':$("#goodsid").val(),'username':$("#usernam
先引入jquery.js 网页前台JS代码 <script type="text/javascript"> function getInfo() { //var id = document.getElementById('goodsid').value; //alert(id); $.ajax({ url : 'ajax2.php',type : 'post',data : {'id':$("#goodsid").val(),'username':$("#username").val()},dataType:'html',//beforeSend:function(){checkpass()},success:function(data){ //alert(data); //将php返回的json转成与之对应的javascript对象方便读取 rdata = $.parseJSON(data); //alert(rdata); //var rdata = {"username" : "mingtao","pass" : "123456"} //alert(rdata.weight); document.getElementById('payfreight').value = rdata.payfreight; document.getElementById('salesprice').value = rdata.salesprice; document.getElementById('totalprice').value = rdata.salesprice; document.getElementById('sprice').value = rdata.salesprice; document.getElementById('protitle').value = rdata.title; document.getElementById('overduedate').value = rdata.weight; } }); } </script> ajax处理后端PHP代码 <?php /* ************************** (C)2010-2014 chinaweber.com update: 2015-8-15 21:59:40 person: xamin ************************** */ /* *持续进修续费返回json */ require_once(dirname(__FILE__).'/include/config.inc.php'); $r = $dosql->GetOne("SELECT * FROM `dede_goods` WHERE id=".$id); $str = array("payfreight"=>$r['payfreight'],"salesprice"=>$r['salesprice'],"title"=>$r['title'],"weight"=>$r['weight']); echo json_encode($str); //将数组转成js可识别的json格式 ?> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |