从example.com/myjsonsql.php提取到jquery mobile的json url无法返回值
我是一名新的php开发人员,我试图创建一个简单的系统,在这个系统中,我使用php从mysql提取数据库,并在jquery mobile中使用json 所以情况是这样的,, 我在我的网站上创建了一个自定义的.php json(从mysql中提取数据),并成功地将其上传到我的网站上,例如:www.example.com/mysqljson.php 这是我提取mysql数据的代码`从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
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错误),例如
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);
}
});
}