Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从example.com/myjsonsql.php提取到jquery mobile的json url无法返回值_Php_Mysql_Json - Fatal编程技术网

从example.com/myjsonsql.php提取到jquery mobile的json url无法返回值

从example.com/myjsonsql.php提取到jquery mobile的json url无法返回值,php,mysql,json,Php,Mysql,Json,我是一名新的php开发人员,我试图创建一个简单的系统,在这个系统中,我使用php从mysql提取数据库,并在jquery mobile中使用json 所以情况是这样的,, 我在我的网站上创建了一个自定义的.php json(从mysql中提取数据),并成功地将其上传到我的网站上,例如:www.example.com/mysqljson.php 这是我提取mysql数据的代码` header('content-type:application/json'); mysql_connect('loc

我是一名新的php开发人员,我试图创建一个简单的系统,在这个系统中,我使用php从mysql提取数据库,并在jquery mobile中使用json

所以情况是这样的,, 我在我的网站上创建了一个自定义的.php json(从mysql中提取数据),并成功地将其上传到我的网站上,例如:www.example.com/mysqljson.php

这是我提取mysql数据的代码`
header('content-type:application/json');

mysql_connect('localhost','root','') or die(mysql_error());

mysql_select_db('mydb');

$select = mysql_query('SELECT * FROM sample');

$rows=array();

while($row=mysql_fetch_array($select))
{
    $rows[] = array('id'=>$row['id'], 'id'=>$row['id'], 'username'=>$row['username'], 'mobileno'=>$row['mobileno'], 'gangsa'=>$row['gangsa'], 'total'=>$row['total']);

}

echo json_encode($rows);`
返回给我以下json@

一切似乎都很好,但当我尝试在jquery mobile上使用json url进行提取时,它不会返回任何值

我使用以下代码提取json

function loadWheather(){
            var forecastURL = "http://example.com/mysqljson.php";

            $.ajax({
                url: forecastURL,
                jsonCallback: 'jsonCallback',
                contentType: "application/json",
                dataType: 'jsonp',
                success: function(json) {
                    console.log(json);
                    $("#current_temp").html(json.id);
                    $("#current_summ").html(json.id.username);      
                },
                error: function(e) {
                    console.log(e.message);
                }
            });
        }
json.id
@#current#temp和
json.id.username
@#current#sum在我的jquery移动页面上不会返回任何结果

我怀疑我没有正确提取json url,但我不确定,有人能帮我找出问题吗


多谢各位

jsonp希望在被检索的json中定义回调函数-例如,请参阅

您的数据打印屏幕实际上是简单的老式json,而不是jsonp

因此,我看到了两种选择:

  • 更改php数据以呈现jsonp(这假设您可以控制数据源)
  • 将jquery请求更改为纯旧的禁食json(这假设客户端和服务器位于同一个域上,否则会发生CORS错误),例如


首先,您不必使用jsonp来检索JSON。您可以只做一个简单的Ajax调用。如果HTTP响应包含良好的内容类型,jQuery会自动将json字符串转换为json对象。您在PHP调用中做得很好

我认为您的问题来自javascript成功回调中的json数据分析。生成的json是一个包含一些用户对象的数组。在代码中,您不处理数组中的一项,而是直接调用数组中的.id或.username。所以javascript给你未定义的值

下面是一个示例,显示json数组的第一项(索引0)的数据。之后,您可以根据自己的目的选择如何处理阵列对象,但这只是一个示例:

function loadWheather(){
        var forecastURL = "http://example.com/mysqljson.php";

        $.ajax({
            url: forecastURL,
            success: function(json) {
                console.log(json);
                // See, I use [0] to get the first item of the array.
                $("#current_temp").html(json[0].id);
                $("#current_summ").html(json[0].username);      
            },
            error: function(e) {
                console.log(e.message);
            }
        });
    }

有人愿意帮忙吗?hi@Sebastian谢谢你的回复,我修改了整个代码并使用了推荐的
$(“#current_temp”).html(json[0].id)
它仍然不会返回任何结果,还有其他建议吗?我是否可以更改json数据的结构,以便可以输入例如
html(json.id.username)
而不是
html(json[0].id)
如果是,那么您可以告诉我应该如何编写json.php脚本。这是我的.php脚本对象结构
$rows[]=array('id'=>$row['id'],'id'=>$row['id'],'username'=>$row['username'],'mobileno'=>$row['mobileno'],'gangsa'=>$row['gangsa'],'total'=>$row['total']);
console.log(json)的输出是什么?感谢@rikAtee的回复。您能告诉我一些关于更改.php数据呈现方式的要点吗?
function loadWheather(){
        var forecastURL = "http://example.com/mysqljson.php";

        $.ajax({
            url: forecastURL,
            success: function(json) {
                console.log(json);
                // See, I use [0] to get the first item of the array.
                $("#current_temp").html(json[0].id);
                $("#current_summ").html(json[0].username);      
            },
            error: function(e) {
                console.log(e.message);
            }
        });
    }