簡體   English   中英

Ajax-無法獲取json數據

[英]Ajax - Can't get json data

我是ajax技術的初學者。 我有一個用於執行mysql_query的PHP,我想在客戶端使用結果。

我的database.php

$q=mysql_query("SELECT name FROM customers");
$res=mysql_fetch_array($q);
echo json_encode($res);

和我的client.php

<div id="output">this element will be accessed by jquery and this text replaced </div>

<script id="source" language="javascript" type="text/javascript">

$(function () 
{
$.ajax({                                      
  url: 'database.php',                  //the script to call to get data          
  data: "",                        

  dataType: 'json',                //data format      
  success: function(data)          //on recieve of reply
  {
    var name = data[0];              

     $('#output').html("<b>id: </b>"+name);

    } 
});
}); 

</script>

這是從我發現的一些教程中獲得的。 正如我看到的database.php 它可以打印正確的數據,但是在client.php我什么也沒得到。 可能是什么問題呢?

---------編輯---------

因此,似乎在Web服務器上運行php 4.4.7,並且json_encode()函數因此無法正常運行。 我找到了“解決方案”。 據我了解,我包括upgrade.php,它為舊版本的php實現了新方法。 這是它的網站http://include-once.org/p/upgradephp/

我無法升級php版本,所以這是一個好的解決方案嗎? 目前,它不起作用

首先嘗試推入或提醒您的價值..如果即將到來,請根據您獲得的價值來做。

 <div id="output">this element will be accessed by jquery and this text replaced </div>

    <script id="source" language="javascript" type="text/javascript">

    $(function () 
    {
    $.ajax({                                      
      url: 'database.php',                  //the script to call to get data          
      data: "",                        

      dataType: 'json',                //data format      
      success: function(data)          //on recieve of reply
      {
       alert(data);  // check data value is coming or not..
        var name = data[0];              

         $('#output').html("<b>id: </b>"+name);

        } 
    });
    }); 

    </script>

是否為您檢查其打印值...

$.ajax({                                      
  url: '/database.php',      //Correct path.                 
  //data: "",                       
  type : 'Get',
  dataType: 'json',                   
  success: function(data)          
  {
     alert(JSON.stringify(data)); 
     var name = data[0];    
     $('#output').html("<b>id: </b>"+name);
  } 
});
$.getJSON("database.php",function(data){
var entries = data;
$.each(entries,function(index,entry){
    //do stuff with json entry here
});
});

應該這樣:

database.php

if (isset($_POST['getData'])) {
    $q=mysql_query("SELECT name FROM customers");
    $res=mysql_fetch_array($q);
    echo json_encode($res);
    die();
}

client.php

<div id="output">this element will be accessed by jquery and this text replaced </div>

<script id="source" language="javascript" type="text/javascript">

$(function () 
{
    $.ajax({                                      
        url: 'database.php',                  //the script to call to get data          
        type: "post",
        data: { getData: true },
        dataType: 'json',                //data format      
        success: function(data)          //on recieve of reply
        {
            var name = data[0];              

            $('#output').html("<b>id: </b>"+name);

        } 
    });
}); 

</script>

在database.php中使用此代碼

    $q=mysql_query("SELECT name FROM customers");

    while($res=mysql_fetch_array($q))
{
$a[]=$res['name'];
}
    echo json_encode($a);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM