【问题标题】:can getJSON() be used for php?getJSON() 可以用于 php 吗?
【发布时间】:2016-05-13 17:25:00
【问题描述】:

这是通过点击加载的 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 页面...我从 [这里][1] 下载了示例源代码

[1]: http://www.sitepoint.com/ajaxjquery-getjson-simple/ 并已在浏览器上运行它,但它仍然无法工作...... 任何帮助将不胜感激!!!谢谢

【问题讨论】:

标签: php jquery json


【解决方案1】:

我今天得到了答案...没有<html>标签应该包含在要输出的代码中...它应该只包含开始和结束的php标签,即

<?php
//your code or codes
?>

【讨论】:

    【解决方案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   
    

    【讨论】:

    • 艾哈迈德先生...我知道这个函数的格式...但我不明白我缺少什么以确保回调
    【解决方案3】:
        <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);
            });
        });
    

    i 是字段名称,字段是值。请检查并回复我。更多详情:-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);
    }
    
    ?>
    

    【讨论】:

    • 先生...感谢您的快速回答....但是回调根本不起作用..这是 load_img.php 的输出....我想这是正确的 bcoz whn i使用 var dump(json_decode) 它也解码了它......
    • 你能告诉我错误吗,因为我知道错误在 php 页面本身......它非常适合 json 文件
    • 你能做到$id=$_GET['id'];再试一次
    • 仍然发生了
    猜你喜欢
    • 2016-12-06
    • 2011-06-09
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    • 1970-01-01
    • 2015-12-30
    • 2014-09-20
    • 1970-01-01
    相关资源
    最近更新 更多