简体   繁体   English

如何使用$ .getJSON从php文件中的JSON数据?

[英]How to JSON data using $.getJSON from a php file?

I have a php file that receives data from mySQL table. 我有一个php文件,可从mySQL表接收数据。 The mySQL table 'user_spec' has only one field 'options' that it returns. mySQL表“ user_spec”只有一个字段“选项”,它返回。 Then I convert returned data into JSON, under is code doing that. 然后,我将返回的数据转换为JSON,下面是执行此操作的代码。

<?php 
 $username = "user"; 
 $password = "********"; 
 $hostname = "localhost"; 
 $dbh = mysql_connect($hostname, $username, $password) or die("Unable to connect  
         to MySQL"); //print "Connected to MySQL<br>"; 
 $selected = mysql_select_db("spec",$dbh) or die("Could not select first_test"); 
 $query = "SELECT * FROM user_spec"; 
 $result=mysql_query($query);  
 echo json_encode(mysql_fetch_assoc($result)); 
?> 

then in an HTML file, I try to output data by this piece of code But it is not working. 然后在HTML文件中,我尝试通过这段代码输出数据,但是它不起作用。 I will be very thankful for any help. 我将非常感谢您的帮助。

<html>
    <head>
    <script type="text/javascript"   
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript" language="javascript">
    function Preload() {
    $.getJSON("Dhttp://localhost/conn_mysql.php", function(json){
    alert("JSON Data: " + json.user_spec);
    });}

    </script></head>
    <body onLoad="Preload()">
    </body>
    </html> 
  1. Jquery missing? jQuery不见了? I assume you gave us a sample of your HTML file and that you have a reference to the jquery script on the page. 我假设您向我们提供了HTML文件的示例,并且您对页面上的jquery脚本有引用。
  2. Syntax error? 语法错误? as Felix wrote - use console of firebug or similar to detect errors. 如Felix所写-使用Firebug控制台或类似控制台来检测错误。
  3. PHP error? PHP错误? try to get the json response using a browser - see you got a response. 尝试使用浏览器获取json响应-看到响应。
  4. Address error? 地址错误? try using absolute link instead of the relative link 尝试使用绝对链接而不是相对链接

Hope this gives you some options to narrow down the issue you arefacing. 希望这给您一些选择来缩小您所面临的问题的范围。

I think instead of using the file adress: "D:xampp/htdocs/conn_mysql.php" you must use an url defined by xampp such as "http://localhost/mytest/conn_mysql.php" 我认为,必须使用xampp定义的网址,例如“ http://localhost/mytest/conn_mysql.php”,而不是使用文件地址“ D:xampp / htdocs / conn_mysql.php

Another thing you need to look out is the method. 您需要注意的另一件事是方法。 $.getJSON ( http://api.jquery.com/jQuery.getJSON/ ), as its name says, works with the GET method. 顾名思义$ .getJSONhttp://api.jquery.com/jQuery.getJSON/ )与GET方法一起使用。 Maybe you should try $.post or $.ajax ( http://api.jquery.com/jQuery.post/ ). 也许您应该尝试$ .post$ .ajaxhttp://api.jquery.com/jQuery.post/ )。

Oh! 哦! And to start your script, not all the browsers supports the <body onload="">. 而且,要启动脚本,并非所有浏览器都支持<body onload =“”>。 Furthermore, it waits for the page to be loaded, not the DOM, what can give you problems with your scripts sometimes. 此外,它等待页面而不是DOM加载,这有时会给您脚本带来问题。 You should use $('document').ready(function() , that waits for the DOM to be loaded. This way: 您应该使用$('document')。ready(function() ,它等待DOM加载。

<script type="text/javascript">
$('document').ready(function()
{

    $.getJSON("D:xampp/htdocs/conn_mysql.php", function(json){
    alert("JSON Data: " + json.options);

});
</script>

I hope it can be useful! 我希望它会有用! ^^ ^^

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM