前端之家收集整理的这篇文章主要介绍了
ajax详细用带返回值,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
先引入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 `#@__goods` WHERE id=".$id);
$str = array("payfreight"=>$r['payfreight'],"salesprice"=>$r['salesprice'],"title"=>$r['title'],"weight"=>$r['weight']);
echo json_encode($str); //将数组转成js可识别的json格式
?>