JSON-如何从JavaScript文件中获取详细信息在javascript中编码JSON数据? [英] JSON-How to get the details from a php file encoded JSON data in a javascript?

查看:56
本文介绍了JSON-如何从JavaScript文件中获取详细信息在javascript中编码JSON数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

details.js



details.js

$(document).ready(function()
{
    $.ajax({
        url: 'js/index.php',
        dataType: 'jsonp',
        jsonp: 'callback',
        timeout: 5000,
        success: function(data){
            $.each(data, function(key,val){
                console.log(val.NAME);
            });
        },
        error: function(){
            alert('There was an error loading the data.');
        }
    });
});





index.php





index.php

<?php
if($_SERVER['REQUEST_METHOD']=="GET")
{
$callback= $_GET['callback'] ;
$db=mysqli_connect("localhost","username","password","detail")or die (" error connection");
$d = mysqli_query($db, "select * from data") or die ("Query error");

while($m = mysqli_fetch_assoc($d))
$output[]=$m;
mysqli_close($db);
$data= json_encode($output);
echo $callback.'('.$data.')';
?>





我正在创建一个cordova应用程序其中我需要从php文件index.php获取数据,详细信息来自数据库mysql。与mysql的连接和插入值等完美完成,这些细节编码为json格式: -

[{ID:1,NAME:abc}] - 这个php文件输出正确显示



现在我需要在javascript文件中获取这些值。我已经尝试了上面的代码,但是在控制台'NAME'中没有显示或者我无法获取详细信息。我怎么能解决这个问题。

任何人都可以帮助我。



I am creating a cordova application in which i need to get the data from a php file index.php ,details are taken from database mysql.The connection with mysql and inserting values etc is perfectly done and these details are encoded to json format:-
[{"ID":"1","NAME":"abc"}]-this output of php file is displayed properly

Now i need to get these values in a javascript file.I have tried the above code,but in the console 'NAME' for example is not displayed or I couldn't get the details.How can i solve this.
Can anybody please help me in this.

推荐答案

(document).ready(function()
{
(document).ready(function() {


.ajax({
url:'js / index.php',
dataType:'jsonp',
jsonp:'callback',
timeout:5000,
成功:函数(数据){
.ajax({ url: 'js/index.php', dataType: 'jsonp', jsonp: 'callback', timeout: 5000, success: function(data){


.each(da ta,function(key,val){
console.log(val.NAME);
});
},
错误:function(){
alert('加载数据时出错。');
}
});
});
.each(data, function(key,val){ console.log(val.NAME); }); }, error: function(){ alert('There was an error loading the data.'); } }); });





index.php





index.php

<?php
if(


这篇关于JSON-如何从JavaScript文件中获取详细信息在javascript中编码JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆