$.ajax() and "Uncaught ReferenceError: data is not defined"
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');
});
Hugolpz
Educational platform Engineer at Center for Research and Interdisciplinarity, Paris. Former PhD candidate in Chinese Teaching and Computer Assisted Language Learning (#CALL), enthusiast wikipedian. I mainly discuss a HTML/CSS/JS for #webapp, #Nodejs, #D3js, #Make, #GIS cartography, #topojson, #wikidata, #openedx. Love to ask short, clean questions on isolated issue with JSfiddle to demo it.
Updated on April 02, 2020Comments
-
Hugolpz about 4 years
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.
-
Hugolpz about 11 yearsDid 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.
-
Hugolpz about 11 yearsOk, my former code hidden a second bug. Line
success: success,
to change intosuccess : 1,
in accordance with my json data. Then your answer works :)