我在后端写了接口调用数据,php写的如下:
public function mlist(){
$sql = "select id,title,author,dateline,lastupdate,picture,keywords,price from items order by id desc limit 10";
$this->db->query($sql);
$ret = $this->db->fetch_array();
echo json_encode($ret);
}
然后我在mui模板内写前端调用这个接口数据:
(function($) {
var methodEl = 'get';
var dataTypeEl = 'html';
var respnoseEl = document.getElementById("list-li");
//成功响应的回调函数
var success = function(response) {
//response = JSON.stringify(response);
//console.log(response);
//respnoseEl.innerHTML = response;
for(var i in response){
console.log(response[i].title);
}
};
var ajax = function() {
//利用RunJS的Echo Ajax功能测试
var url = 'http://www.miledou.com/index.php?m=content&c=mui&a=mlist';
//请求方式,默认为Get;
var type = methodEl;
//预期服务器范围的数据类型
var dataType = dataTypeEl;
respnoseEl.innerHTML = '正在请求中...';
$.get(url,success);
};
ajax();
mui.plusReady(function(){
//关闭等待框
plus.nativeUI.closeWaiting();
//显示当前页面
mui.currentWebview.show();
});
})(mui);
拿不到循环出来的数据,整体log出来是这样的:
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/><title>[1/2] </title><script type="text/javascript">function _conoff(id){var d=document.getElementById(id);var a=document.getElementById(id+"_a");if(d&&a){d.style.display=d.style.display=="none"?"block":"none";a.innerHTML=d.style.display=="none"?a.innerHTML.replace(/\-/g,"+"):a.innerHTML.replace(/\+/g,"-");}}</script><style type="text/css">body{font-size:15px;margin:3px;padding:0px;line-height:150%;} img{border:0px;} form{margin-top:0px;margin-bottom:0px;} table{font-size:15px;border:0px;} input{font-size:15px;} hr{color:#666666;} small{font-size:100%;} big{font-size:110%;}</style></head><body>[{"id":"1187","title":"\u5973\u58eb\u51ac\u88c5\u52a0\u539a\u7fbd\u7ed2\u4e2d\u957f\u6b3e2014\u65b0\u6b3e\u4fee\u8eab\u5973\u7fbd\u7ed2\u8fde\u5e3d\u5916\u5957\u827e\u5a03\u4f9d\u59ae","author":"35425506917","dateline":"1418664972","lastupdate":"1418664972","picture":"\/go\/img.php?uuu=i1\/T1JDdyFA8cXXXXXXXX_!!0-item_pic.jpg_220x220.jpg","keywords":"\u7fbd\u7ed2\u5973","price":"599.00"},{"id":"1186","title":"\u5962\u534e\u4fee\u8eab\u663e\u7626\u7fbd\u7ed2\u68c9\u670d\u5973\u8d85\u957f\u6b3e\u8fc7\u819d\u52a0\u539a\u5927\u6bdb\u9886\u8fde\u5e3d2014\u65b0\u6b3e\u6f6e\u97e9\u7248","author":"42590597641","dateline":"1418664969","lastupdate":"1418664969","picture":"\/go\/img.php?uuu=i2\/TB1CaiFGFXXXXXmaXXXXXXXXXXX_!!0-item_pic.jpg_220x220.jpg","keywords":"\u7fbd\u7ed2\u5973","price":"266.00"},{"id":"1185","title":"\u6f6e2014\u51ac\u7fbd\u7ed2\u68c9\u670d\u5973\u4e2d\u957f\u6b3e\u52a0\u539a\u5b66\u751f\u51ac\u88c5\u5916\u5957\u68c9\u8863\u5973\u68c9\u8884\u5973\u97e9\u7248","author":"42404075522","dateline":"1418664968","lastupdate":"1418664968","picture":"\/go\/img.php?uuu=i1\/TB1eiwgGFXXXXcSXVXXXXXXXXXX_!!0-item_pic.jpg_220x220.jpg","keywords":"\u7fbd\u7ed2\u5973","price":"178.00"},{"id":"1184","title":"\u97e9\u7248\u5916\u5957\u4f11\u95f2\u77ed\u6b3e\u65f6\u5c1a\u62fc\u8272\u52a0\u539a\u8fde\u5e3d\u519b\u5de5\u88c5\u65b0\u6b3e\u7fbd\u7ed2\u68c9\u670d\u59732014\u51ac\u88c5","author":"41509160047","dateline":"1418664966","lastupdate":"1418664966","picture":"\/go\/img.php?uuu=i2\/TB1c0D0GVXXXXXdXXXXXXXXXXXX_!!0-item_pic.jpg_220x220.jpg","keywords":"\u7fbd\u7ed2\u5973","price":"188.00"},{"id":"1183","title":"\u65b0\u6b3e\u4fee\u8eab\u957f\u6b3e\u97e9\u7248\u7fbd\u7ed2\u670d\u5973\u52a0\u539a\u4fdd\u6696\u8fde\u5e3d\u7fbd\u7ed2\u68c9\u670d\u5916\u5957shezgood","author":"42660666697","dateline":"1418664963","lastupdate":"1418664963","picture":"\/go\/img.php?uuu=i3\/TB13q1hGVXXXXakXFXXXXXXXXXX_!!0-item_pic.jpg_220x220.jpg","keywords":"\u7fbd\u7ed2\u5973","price":"369.00"},{"id":"1182","title":"\u8f7b\u8584\u6b3e\u5973\u88c5\u4f18\u8863\u5e93\u7fbd\u7ed2\u670d\u5973\u8fde\u5e3d\u4fee\u8eab\u5927\u7801\u51ac\u88c5\u80d6\u59b9\u59b9\u8f7b\u7fbd\u7ed22014\u65b0\u6b3e","author":"41830356094","dateline":"1418664961","lastupdate":"1418664961","picture":"\/go\/img.php?uuu=i2\/TB1hVLTGFXXXXXgX
解决怎么处理数据?ajax具体用法是怎么样的?如果像jquery那样用我会用,现在卡到这了555555555
4 个回复
DCloud_UNI_FXY
mui.getJSON调用。
服务端返回时指定正确的content-type
longlong (作者) - 自信可爱!
谢谢了,根据大牛的指导在自己服务端加了一句这个:
header("Content-type:application/json; charset=utf-8");
搞定了!
p***@qq.com
我用MUI.AJAX來回傳PHP數據, 但出現錯誤, "this site requires javascript to work",其實我想直接把數據傳回網頁,要如何呢!!請指教...
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<title></title>
<script src="js/mui.min.js"></script>
<link href="css/mui.min.css" rel="stylesheet"/>
<script type="text/javascript" charset="utf-8">
mui.init();
</script>
</head>
<body>
<script>
function showHint(str) {
if (str.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
});
}
</script>
</head>
<body>
<p><b>Start typing a name in the input field below:</b></p>
<form>
First name: <input type="text" onkeyup="showHint(this.value)">
</form>
<p>Suggestions: <span id="txtHint"></span></p>
</body>
</html>
l***@wo.com
为什么 mui ajax都会调用两次?