jQuery $.ajax() 和“未捕获的 ReferenceError:数据未定义”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15743483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:42:58  来源:igfitidea点击:

$.ajax() and "Uncaught ReferenceError: data is not defined"

javascriptjqueryajaxjsongetjson

提问by Hugolpz

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

我尝试了几种使用 $.getJSON 和 $.ajax()获取 .json文件和数据的方法

My JS coden?2 fails :

我的 JS 代码n?2 失败:

$.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:

在 Chrome 控制台中,消息错误是

"Uncaught ReferenceError: data is not defined"

Refering tothe line :

指的线路:

  data: data,

What is going wrong ? What to do ?

出了什么问题?该怎么办 ?



Edit:all is done client side.

编辑:一切都在客户端完成。

回答by Daniel Imms

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

问题是由于您没有定义变量数据引起的,因此请尝试删除该data: data行,看起来您只是获得了一个通常不会采用查询字符串的 JavaScript 文件:

$.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');
});