3

I tried out several ways to get .json file and data using $.getJSON and $.ajax() overthere

My JS code n⁰2 fails :

$.ajax({
  type: "GET",
  url: 'js/main.js',
  data: data,
  success: 1,
  }).done(function ( data ) {
  var items = [];

  $.each(data.tata.entities.q142.labels.fr.value, function(key, val) {
    items.push('<li id="' + key + '">Test 2:' + val + '</li>');
  });

  $('<ul/>', {
    'class': 'my-new-list',
    html: items.join('')
  }).appendTo('body');
});

In Chrome console, the message error is :

"Uncaught ReferenceError: data is not defined"

Refering to the line :

  data: data,

What is going wrong ? What to do ?


Edit: all is done client side.

2
  • Do you have a variable somewhere where the data you want to send to the server is stored? It's trying to GET with some data attached in the form of a querystring ?something=blah Commented Apr 1, 2013 at 12:30
  • 1
    Do you have any variable with name as data ? if so please post that variable's data Commented Apr 1, 2013 at 12:31

1 Answer 1

9

The problem is being caused because you didn't define the variable data, so try removing the data: data line, it looks like you're just getting a JavaScript file which wouldn't normally take a query string:

$.ajax({
  type: "GET",
  url: 'js/main.js',
  success: success,
  }).done(function ( data ) {
  var items = [];

  $.each(data.tata.entities.q142.labels.fr.value, function(key, val) {
    items.push('<li id="' + key + '">Test 2:' + val + '</li>');
  });

  $('<ul/>', {
    'class': 'my-new-list',
    html: items.join('')
  }).appendTo('body');
});
Sign up to request clarification or add additional context in comments.

2 Comments

Did it work for you ? It didn't for me. I'am digging around your idea, maybe some conflicts with my Code 1, Code 3, Code 4.
Ok, my former code hidden a second bug. Linesuccess: success, to change into success : 1, in accordance with my json data. Then your answer works :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.