PHP脚本json_en code MySQL的请求无法传递到的getJSON() [英] PHP script json_encode mysql request cannot pass through to getJSON()

查看:240
本文介绍了PHP脚本json_en code MySQL的请求无法传递到的getJSON()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

真的注意,从大师的帮助在下面的神秘面纱。

我用的getJSON()在我的HTML。

只有硬codeD数组可以json_en code(通过设置即 $ DEBUG = TRUE :),并传递给JavaScript和浏览器随后显示结果。但是,当失败产生从MySQL文本(通过设置 $ DEBUG = FALSE )。

我抓我的头获得MySQL生成的动态数组工作?我可以在浏览器中JSON格式化文本输出浏览器同时运行的情况下,即 HTTP ://www.example.com/phpTWLLT/json_en$c$cd_array.php

如果 $ DEBUG 设置真正

从本地主机/ phpTWLLT / json_en code_array.php

输出<$p$p><$c$c>[{\"active\":\"0\",\"first_name\":\"Darian\",\"last_name\":\"Brown\",\"age\":\"28\",\"email\":\"darianbr@example.com\"},{\"active\":\"1\",\"first_name\":\"John\",\"last_name\":\"Doe\",\"age\":\"47\",\"email\":\"john_doe@example.com\"}]

在浏览器中显示的列表。
0
1

如果 $ DEBUG 设置

从本地主机/ phpTWLLT / json_en code_array.php

输出

  [{积极:1},{积极:1}]

浏览器显示为空白。

HTML文件:

 &LT;!DOCTYPE HTML&GT;
&LT;! -
为了改变这种许可证标头,选择项目属性许可头。
要改变这种模板文件,选择工具|模板
并打开编辑器的模板。
- &GT;
&LT; HTML和GT;
    &LT; HEAD&GT;
        &LT;! -
        &LT;脚本类型=文/ JavaScript的SRC =HTTP://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'> &LT; / SCRIPT&GT;
         - &GT;
    &LT;脚本类型=文/ JavaScript的'SRC ='JS / jquery.min.js'&GT;&LT; / SCRIPT&GT;    &LT;间的charset =UTF-8&GT;
    &LT; /头&GT;
    &LT;身体GT;    &LT;! - 这UL将与来自PHP数组中的数据进行填充 - &GT;
    &LT; UL&GT;&LT; / UL&GT;    &LT;脚本类型=文/ JavaScript的'&GT;
        $(文件)。就绪(函数(){
            / *调用具有PHP数组是json_en codeD *的PHP /
            //$.getJSON('json_en$c$cd_array.php'功能(数据){
            $ .getJSON('json_en coded_array.php'功能(数据){
                / *数据将持有PHP数组作为一个JavaScript对象* /                 $。每个(数据,功能(键,VAL){                 $(UL)追加。('&LT;李ID =+按键+'&GT;'+ val.active +'&LT; /李&GT;');
                 });            });
        });
        &LT; / SCRIPT&GT;    &LT; /身体GT;
&LT; / HTML&GT;

PHP脚本:json_en coded_array.php

 &LT; PHP/ *
 *要改变这种许可证标头,选择项目属性许可头。
 *要改变这个模板文件,选择工具|模板
 *并打开编辑器的模板。
 * // *载文档类型为text / JavaScript的,而不是text / html的* /
$ DEBUG = TRUE;如果($ DEBUG){
    标题(内容类型:text / JavaScript的);
    $ ARR =阵列(
        阵列(
            活动= GT; 0,
            FIRST_NAME=&GT; 达里安
            姓氏=&GT; 棕色,
            时代=&GT; 28,
            电子邮件=&GT; darianbr@example.com
        )
        阵列(
            活动= GT; 1,
            FIRST_NAME=&GT; 约翰,
            姓氏=&GT; 李四,
            时代=&GT; 47,
            电子邮件=&GT; john_doe@example.com
        )
    );
}其他{
    require_once('connection.php');
// $ m_id = 8有许多就读课程,并得到了11正好一门课程就读。
    $ m_id = 8;
    $ P_ID = 1;    $ qry1 =SELECT DISTINCT event.active为活动,主题。code作为当然code',subject.name为课程名,event.event_desc为eventDesc'FROM申请人,事件,主题,部分其中,applicant.applicant_id = $ m_id和applicant.event_id = event.id和event.subject_id = subject.id和part.id = subject.owner_id和part.id = $ P_ID由event.active DESC,event.from_month DESC命令;
    mysqli_set_charset($ BD,'utf-8');
    $结果= mysqli_query($ BD,$ qry1);    $ ARR =阵列();
    $ I = 0;
    如果(mysqli_num_rows($结果)大于0){
        而($ RS = mysqli_fetch_assoc($结果)){
             $ colhead =主动;
            $海峡= $ RS ['主动'];            $常用3 [$ i] =阵列($ colhead =&GT; $海峡);
            $ I ++;                //只是生成两个记录进行测试
                如果($ I === 2)
                打破;        }
    }
}
回声json_en code($ ARR);
?&GT;


解决方案

您需要一个头在PHP脚本添加到输出为 JSON :json_en coded_array .PHP

 标题(内容类型:应用程序/ JSON);

默认情况下

PHP将返回的text / html 这是不是 $有效的JSON。的getJSON()

有关JSON文本的MIME媒体类型是application / JSON。默认的编码是UTF-8。 (来源: RFC 4627

Really note help from guru on the below mystery.

I used getJSON() in my html.

Only hardcoded array can be json_encode (i.e. by setting $DEBUG = true:) and pass to javascript and subsequently browser display the result. But fail when generate the text from mysql (by setting $DEBUG = false).

I am scratching my head to get mysql generated dynamic array to work? I can run both scenario in the browser with JSON formated text output in the browser, i.e http://www.example.com/phpTWLLT/json_encoded_array.php.

If $DEBUG is set true,

output from localhost/phpTWLLT/json_encode_array.php

[{"active":"0","first_name":"Darian","last_name":"Brown","age":"28","email":"darianbr@example.com"},{"active":"1","first_name":"John","last_name":"Doe","age":"47","email":"john_doe@example.com"}]

the list displayed in browser. 0 1

If $DEBUG is set false,

output from localhost/phpTWLLT/json_encode_array.php

[{"active":"1"},{"active":"1"}]

The browser display is blank.

html file:

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <!--
        <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js'>    </script>
        -->
    <script type='text/javascript' src='js/jquery.min.js'></script>

    <meta charset="UTF-8">
    </head>    
    <body>

    <!-- this UL will be populated with the data from the php array -->
    <ul></ul>

    <script type='text/javascript'>
        $(document).ready(function () {
            /* call the php that has the php array which is json_encoded */
            //$.getJSON('json_encoded_array.php', function(data) { 
            $.getJSON('json_encoded_array.php', function (data) {
                /* data will hold the php array as a javascript object */

                 $.each(data, function (key, val) {

                 $('ul').append('<li id="' + key + '">' + val.active  + '</li>');
                 });

            });
        });
        </script>

    </body>
</html>

PHP script: json_encoded_array.php

<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */



/* set out document type to text/javascript instead of text/html */


$DEBUG = true;

if ($DEBUG) {
    header("Content-type: text/javascript");
    $arr = array(
        array(
            "active" => "0",
            "first_name" => "Darian",
            "last_name" => "Brown",
            "age" => "28",
            "email" => "darianbr@example.com"
        ),
        array(
            "active" => "1",
            "first_name" => "John",
            "last_name" => "Doe",
            "age" => "47",
            "email" => "john_doe@example.com"
        ) 
    );
} else {
    require_once('connection.php');
// $m_id= 8 has many enrolled course and 11 got exactly one course enrolled. 
    $m_id = 8;
    $p_id = 1;

    $qry1 = "SELECT distinct event.active as active, subject.code as 'courseCode', subject.name as     'courseName', event.event_desc as 'eventDesc' FROM applicant, event, subject, part where applicant.applicant_id = $m_id and applicant.event_id = event.id and event.subject_id=subject.id and part.id = subject.owner_id and part.id = $p_id order by event.active DESC, event.from_month DESC ";
    mysqli_set_charset($bd, 'utf-8');
    $result = mysqli_query($bd, $qry1);

    $arr = array();
    $i = 0;
    if (mysqli_num_rows($result) > 0) {
        while ( $rs = mysqli_fetch_assoc($result)  ) {
             $colhead = "active";
            $str = $rs['active'];

            $arr[$i] = array($colhead => $str);
            $i++;

                // just generate two record for testing
                if ($i === 2)
                break;

        }
    } 
}
echo json_encode($arr);
?>

解决方案

You need to add a header to output it as json in PHP script: json_encoded_array.php

 header("Content-type: application/json");

By default PHP will return text/html which is not a valid JSON for $.getJSON()

The MIME media type for JSON text is application/json. The default encoding is UTF-8. (Source: RFC 4627).

这篇关于PHP脚本json_en code MySQL的请求无法传递到的getJSON()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆