-2

UPDATED

This is my structure:

FILE.json

{
    "Page": [
        {
            "Id": 0,
            "IsActive":true,
            "PageName":"crosstownrunning",
            "PageURL":"crosstownrunning"
        }    
    ]
}

FILE.js

$.noConflict();
jQuery(document).ready(function ($) {

// VARS
var PageTemplate;

// GET JSON
jQuery.getJSON("FILE.json", function (x) {
    // alert(x.Page[0].PageName);  << This works
    PageTemplate = x.Page[0];
});

alert (PageTemplate.PageName);
alert (PageTemplate.PageURL);
// Can I make PageTemplate.anything bring up my JSON data outside getJSON function?

});

How can I call the x outside the function?

Community
  • 1
  • 1
TheBlackBenzKid
  • 26,324
  • 41
  • 139
  • 209
  • Turn on the debugger and check that the request for the file is sent. Also check that your file is in the same path as your js file as you have not specified a complete URI – vogomatix Mar 07 '14 at 11:33
  • What are you expecting as result if more than one page? Anyway in your code `Page` is undefined, x is the object. Question could be, why Page is an array? – A. Wolff Mar 07 '14 at 11:33
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Satpal Mar 07 '14 at 11:37

2 Answers2

1
jQuery.getJSON("FILE.json", function (x) {
    alert(x.Page[0].PageName);
});
0

You need to use the callback function since jQuery.getJSON function is asynchronous.

jQuery.getJSON("FILE.json", function(x) {
    $('.emptyBox').append(x.Page[0].PageURL);
});
letiagoalves
  • 11,224
  • 4
  • 40
  • 66