开春大礼《华为云技术精选集》大厂100+前沿技术实战分享!>>>
各位好,我遇到了一个难题,我想在前台用jquery的getJSON想获取json数据。现在遇到的麻烦是,我测试了有些浏览器可以正常执行(chrome跟safari可以),有些浏览器却不能(360跟IE不可以)。下面贴出代码:
index.html:
<html>
<head>
<title>5S</title>
<!--
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
<script src="js/index.js"></script>
-->
<link rel="stylesheet" href="css/jquery.mobile-1.3.2.min.css">
<link rel="stylesheet" href="css/index.css">
<script src="js/jquery-1.8.3.min.js"></script>
<script src="js/jquery.mobile-1.3.2.min.js"></script>
<script src="js/index.js"></script>
</head>
<body>
<div data-role="page" id="fm_gs_show">
<div data-role="header">
<a href="#" data-role="button" data-rel="back" data-icon="back">返回</a>
<h1 id="fm_gs_show_h">改善图例</h1>
<a href="#home" data-role="button" data-icon="home">首页</a>
<div data-role="navbar">
<ul>
<li><a href="#fm_gs">改善图例</a></li>
<li><a href="#fm_cp">检查项目</a></li>
<li><a href="#fm_ck">检查记录</a></li>
<li><a href="#fm_zl">相关资料</a></li>
<li><a href="#fm_pf">评分</a></li>
</ul>
</div> <!--navbar -->
</div> <!--header -->
<div data-role="content">
<div class="fm_gs_show_imgdiv" id="fm_gs_show_imgdiv">
</div>
</div> <!--content -->
<div data-role="footer">
<h2>Footer</h2>
</div> <!--footer -->
</div> <!--page -->
</body>
</html>
index.js:
pv_gsshowdept = "";
$(document).ready(function(){
$("#fm_gs_show").bind('pagebeforeshow', function(event) {
//alert(pv_gsshowdept);
$$("fm_gs_show_h").innerHTML = pv_gsshowdept;
$("#fm_gs_show_imgdiv").empty();
var myjson = "http://192.168.0.30:81/fkfs/jsons/json_deptimprove.php?dept="+$.trim(pv_gsshowdept);
// console.log("json语句是:"+myjson);
alert("json语句是:"+myjson);
$.getJSON(myjson,
function(data) {
$.each(data, function(k, v) {
// alert(v);
var mystr = " <div> <img src='files/pr/"+$.trim(pv_gsshowdept)+"/"+v+"' style='width:100%; height:auto;' /> <p>"+v+"</p> </div>" ;
// console.log(v+mystr);
$("#fm_gs_show_imgdiv").append(mystr);
})
}); //getJSON end
});
});
json_deptimprove.php:
<?php
$con = mysql_connect('127.0.0.1:3309','root','123456'); // 连接MYSQL
if (!$con) {
die('Could not connect to MySQL: ' . mysql_error());
}
mysql_query("set names 'utf8'",$con); //防止乱码
//echo function_exists('iconv') ? 'yes' : 'no';
mysql_select_db("fkks", $con); //选择数据库
$str = trim($_GET['dept']);
$mysql ="insert into test(tt) values('".$str."')";
$result = mysql_query($mysql); ;
$mysql ="SELECT pname FROM deptimprove where dept like '%".$str."%'";
//echo $mysql;
$result = mysql_query($mysql);
//[{"dept":"QA"},{"dept":"DY"}]
//$testJSON=array("dept"=>"".$row['dept']."");
$testJSON=array();
while($row = mysql_fetch_array($result))
{
$testJSON[]=$row['pname'];
}
// print_r($testJSON);
foreach ( $testJSON as $key => $value ) {
$testJSON[$key] = urlencode ( $value );
}
echo urldecode ( json_encode ( $testJSON ) );
mysql_close($con); //关闭数据
?>
代码就是这么多了,我为了验证,在PHP文件里加了一段插入数据库的功能,能访问的浏览器可以正常插入数据,不能的就没有插入。我都困扰了,谢谢各位。
原生js的时候我是这样用的,js端:
在php的这端:console.log都会用, 还查不到原因.
楼主, 你是低端黑php呀...
引用来自“Tsui”的答案
原生js的时候我是这样用的,js端:
在php的这端: