admin管理员组

文章数量:1252691

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.

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.

Share Improve this question edited Apr 1, 2013 at 14:56 Hugolpz asked Apr 1, 2013 at 12:28 HugolpzHugolpz 18.3k28 gold badges105 silver badges191 bronze badges 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 – Henrik Andersson 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 – Karthik Chintala Commented Apr 1, 2013 at 12:31
Add a ment  | 

1 Answer 1

Reset to default 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');
});

本文标签: javascriptajax() and quotUncaught ReferenceError data is not definedquotStack Overflow