Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
details.js

XML
$(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

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.')';
?>


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.
Posted
Updated 1-Aug-14 1:20am
v4
Comments
SrikantSahu 1-Aug-14 11:44am    
Jquery has a property 'd' for the returned ajax results.
so the following code should work.
$.each(data.d, function(key,val){
console.log(val.NAME);
});
p@y@l 2-Aug-14 0:39am    
Thanks for your help i got it! :)
pandu web dev 13-Aug-14 5:27am    
You can try jquery json reading plugins. For reading json easily.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900