开发者

JQuery JSON read and print $.getJSON

开发者 https://www.devze.com 2023-02-07 06:12 出处:网络
This is something new to me so please be patient. JQuery & json- I have been using json & serializefor \"ages\" in php but just getting into JQ.I w开发者_JS百科ant to pass data cross domain- s

This is something new to me so please be patient. JQuery & json - I have been using json & serialize for "ages" in php but just getting into JQ. I w开发者_JS百科ant to pass data cross domain- so json array seems best to me but how to I display it e.g.

say I have a json array like this created in php on page name examplearray.php located at http://domain1.com/examplearray.php

    $catarray = array('Animal Life','Business & Finance','Cars & Vehicles','Entertainment & Arts','Food & Cooking','Health','History, Politics & Society','Hobbies & Collectibles','Home & Garden','Humor & Amusement','Jobs & Education','Law & Legal Issues','Literature & Language','Relationships','Religion & Spirituality','Science','Shopping','Sports','Technology','Travel & Places'); 
$callback =  json_encode($catarray);

I understand I can call it like this from another domain say http://domain2.com

$(document).ready(function(){
$.getJSON("http://domain1.com/examplearray.php?callback=",
function(data){
$.each(data, function(i,item)
{  
// WHAT DO I put in here to read the array? i.e. what are the keys etc. //
// I assume the display is something like $('#element').html('something');
});
});
});

In the above example there are no "set" keys if you follow that , but now add a key to the array like this

$catarray = array('keyname' => array('Animal Life','Business & Finance','Cars & Vehicles','Entertainment & Arts','Food & Cooking','Health','History, Politics & Society','Hobbies & Collectibles','Home & Garden','Humor & Amusement','Jobs & Education','Law & Legal Issues','Literature & Language','Relationships','Religion & Spirituality','Science','Shopping','Sports','Technology','Travel & Places'));

Help much appreciated, thanks.


You need to add a ? after your callback and you need to wrap your code with that callback server-side:
examplearray.php:

<?php
// your data generation routine
$results = array('Animal Life','Business &amp; Finance','Cars &amp; Vehicles','Entertainment &amp; Arts','Food &amp; Cooking','Health','History, Politics &amp; Society','Hobbies &amp; Collectibles','Home &amp; Garden','Humor &amp; Amusement','Jobs &amp; Education','Law &amp; Legal Issues','Literature &amp; Language','Relationships','Religion &amp; Spirituality','Science','Shopping','Sports','Technology','Travel &amp; Places');
if(isset($_REQUEST['callback'])) {
    $json_callback_func = $_REQUEST['callback'];
    echo $json_callback_func . '('.json_encode($results).')';
}
?>

And your JS:

$(function() {
    $ul = $('ul');
    $.getJSON("http://domain1.com/examplearray.php?callback=?", function(data){
        $.each(data, function(k,v){
            $ul.append("<li>" + v + "</li>");
        });
    });
});

Now since your array is with no indexes it's a numerical array, so the keys k will be the index only.

Now if you have keys, like the second example of yours, you may want to use nested loops or if you have the exact case as your example then use:

$.each(data.keyname, function(k,v){


To understand what variables you have to work with, you can try running your code in a debugger like "Javascript Console" in Chrome, or Firebug in Firefox. Put a breakpoint in the $.each function. Look at your local variables and you should see all the data structures available to you. Get used to using the debugging tools in browsers. They save you loads of time.

To address your question more directly, there are good examples for accessing data using $.each() here: http://api.jquery.com/jQuery.getJSON/

"item" is now a javascript object in your example. You can use it as such (item.attribute)

0

精彩评论

暂无评论...
验证码 换一张
取 消