这是通过点击加载的JQuery代码,但回调函数不起作用:
$.getJSON("load_img.php", {"id":"start"} , function(json){
alert(json);
});
PHP代码为我提供了这个输出:
[
{"img_name":"shiva.jpg","img_id":"1"},
{"img_name":"shiva.jpg","img_id":"2"},
{"img_name":"Recoverd_jpg_file(4).jpg","img_id":"3"},
{"img_name":"Recoverd_jpg_file(542).jpg","img_id":"4"}
]
我无法从getJSON()函数加载任何PHP页面...我从[here]下载了示例源代码[1]
[1]:http://www.sitepoint.com/ajaxjquery-getjson-simple/并在浏览器上运行它,但它仍然无效... 任何帮助将不胜感激!!!谢谢
答案 0 :(得分:1)
我今天得到了答案......要输出的代码中不应包含<html>
标签......它应该只包含起始和结束的php标签,即
<?php
//your code or codes
?>
答案 1 :(得分:-2)
<button id="try" type="button">Try</button>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
$.getJSON("load_img.php", {"id":"start"}, function(result){
$.each(result, function(i, field){
alert(field.img_name);
});
});
我是字段名称,字段是值。请检查并发回给我。有关详细信息: - http://www.w3schools.com/jquery/ajax_getjson.asp
第二页: -
<?php
// ini_set("display_errors", "On");
//header('content-type:application/json');
$id = $_GET["id"];
if ($id == "start")
{
$con=new mysqli("localhost", "root", "rootwdp", "try");
$numbers = array();
$img_arr = array();
$res = $con->query("select img_name, img_id from img");
while ($row = $res->fetch_assoc())
// mysql_fetch_array() passes parameters from 0
{ $numbers[]=$row; }
echo json_encode($numbers);
}
?>
答案 2 :(得分:-2)
$.getJSON("path to php file", {method: "*", "id":"start"})
.done(function (data) {
console.log(data)
});
//================in php=================================
if($_REQUEST['method'] == "your method" AND $_REQUEST['id'] ) {
//call your php function
}
//-------------------
//and put your php code in function
//.. i hope this help you