简体   繁体   English

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

[英]Extracted json url from example.com/myjsonsql.php to jquery mobile fail to return value

i am a new php developers i was trying to create a simple system where i use php to extract database from mysql and use json in jquery mobile. 我是一个新的PHP开发人员,我试图创建一个简单的系统,在该系统中,我使用php从mysql提取数据库并在jquery mobile中使用json。

So here is the situation, I've created a custom .php json (to extract data from mysql) on my website and i've successfully upload it onto my website eg: www.example.com/mysqljson.php 因此,在这种情况下,我在我的网站上创建了一个自定义.php json(从mysql中提取数据),并将其成功上传到我的网站上,例如:www.example.com/mysqljson.php

This is my code extracting mysql data ` 这是我提取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);`

Which in returns gives me the following json @ http://i.imgur.com/d4HIxAA.png?1 返回哪一个给我以下json @ http://i.imgur.com/d4HIxAA.png?1

Everything seems fine, but when i try to use the json url for extraction on jquery mobile it doesn't return any value. 一切似乎都很好,但是当我尝试使用json网址在jquery mobile上进行提取时,它不会返回任何值。

i extract the json by using the following code; 我通过使用以下代码提取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);
                }
            });
        }

The json.id @ #current_temp and json.id.username @ #current_sum dint return any result on my jquery mobile page. json.id @ #current_temp和json.id.username @ #current_sum dint返回我的jquery移动页面上的任何结果。

I suspected i didn't extracted the json url correctly, but im not sure, could anyone help me identify the problem? 我怀疑我没有正确提取json url,但是我不确定,有人可以帮助我确定问题吗?

Thank you. 谢谢。

jsonp expects a callback function to be defined in the json being retrieved - for ecample see here . jsonp期望在要检索的json中定义一个回调函数-有关详细信息,请参见此处

Your data print screen is actually plain old fashioned json, not jsonp. 您的数据打印屏幕实际上是普通的老式json,而不是jsonp。

So I see 2 options: 所以我看到2个选项:

  • change the php data to render jsonp (this assumes you have control over data source). 将php数据更改为呈现jsonp(这假设您可以控制数据源)。
  • change your jquery request to expect plain old fastioned json (this assumes client and server are on same domain, otherwise CORS error happen), eg,, 更改您的jquery请求,以期望使用普通的老式json(这假定客户端和服务器位于同一域,否则会发生CORS错误),例如,

      $.get(forecastURL) .then(function(json) { console.log(json); $("#current_temp").html(json.id); $("#current_summ").html(json.id.username); }) .fail(function(e) { console.log(e.message); }); 

First of all, you don't have to use jsonp to retrieve JSON. 首先,您不必使用jsonp来检索JSON。 You could just do a simple Ajax Call. 您可以做一个简单的Ajax调用。 jQuery convert automatically your json string to a json object if your HTTP response contains the good Content-Type. 如果您的HTTP响应包含良好的Content-Type,则jQuery自动将您的json字符串转换为json对象。 And you did this good in your PHP call. 您在PHP调用中做到了这一点。

I think your problem comes from your json data analysis in your javascript success callback. 我认为您的问题来自您的JavaScript成功回调中的json数据分析。 The json generated is an array containing some user objects. 生成的json是包含一些用户对象的数组。 In your code you don't work on one item of the array but you directly call .id or .username on the array. 在您的代码中,您无法处理数组的一项,但是可以直接在数组上调用.id或.username。 So javascript give you undefined values. 因此,javascript给您未定义的值。

Here is an example displaying the data for the first item (index 0) of your json array. 这是一个显示json数组的第一项(索引0)的数据的示例。 You have after that to choose for your own purpose how to treat your array objects but it's an example: 之后,您可以根据自己的目的选择如何处理数组对象,但这是一个示例:

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);
            }
        });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Nginx RewriteURL从example.com/script.php?param=value到example.com/value - Nginx rewriteurl from example.com/script.php?param=value to example.com/value 如何将example.com/index.php#anchor的URL重写为example.com/anchor? - How do I rewrite the URL for example.com/index.php#anchor to example.com/anchor? 如何处理从example.com/search/?s=some到example.com/search/some的URL - How to manipulate the URL from example.com/search/?s=some to example.com/search/some PHP正则表达式可匹配Example.com之外的任何URL - PHP Regular Expression To Match Any URL Except Those From Example.com 检测URL标记example.com/page.php#anycontent - Detect URL tags example.com/page.php#anycontent 如何从这个example.com/index.php?lang=en更改我的多语言网址到这个example.com/en/? - How can I change my multilingual urls from this example.com/index.php?lang=en to this example.com/en/? Apache 2阻止网址,例如example.com/?admin - Apache 2 block url such as example.com/?admin example.herokuapp.com 重定向到 example.com - PHP - example.herokuapp.com redirection to example.com - PHP 如果网址中没有index.php,example.com / index.php中的首页无法加载 - Home page is not loading without index.php in url, example.com/index.php URL重写-如何将example.com/pageid.php?id=12转换为example.com/full-title - URL rewriting- How can I turn example.com/pageid.php?id=12 to example.com/full-title
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM