javascript - 在 error.responeText 中取回 JSON 数据

标签 javascript jquery html css json

我正在尝试从服务器上的文件中获取一些 JSON 数据,但出于某种原因它无法正常工作,但出现了错误。但是如果我记录 error.responseText 我可以在那里看到我的数据!?

我正在使用这个功能

function getJson(url) {

var result;
 $.getJSON(url, function(data){

    console.log('success');

}).error(function(error){

    result = error.responseText;
    console.log("Error: " + result);

    return result;
});

有人知道为什么吗?

最佳答案

你必须使用失败函数。

function getJson(url) {

  var result;
  $.getJSON(url, function(data){

   console.log('success');

  }).fail(function(jqxhr, textStatus, error){

  result = textStatus + ", " + error;
  console.log("Error: " + result);

  return result; 
});

关于javascript - 在 error.responeText 中取回 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24909235/

相关文章:

javascript - 在 html 文件中包含 no cache 元标记是否也会阻止缓存该 html 文件中链接的 js 和 css 文件?

javascript - 为什么 sails.js 忽略/config/env/production.js 而使用 process.env.PORT 代替?

javascript - pageinit 内的单击事件仅在页面刷新后起作用

javascript - amphtml 标记显示错误 "Referenced AMP URL is not an AMP"

javascript - 在 Chrome 中调整绘图 Canvas 的大小

javascript - JS : Making prototype function

javascript - 宽高比为 16 :9 and 4:3/Vertically centering the canvas when the aspect ratio is 4:3 的 Canvas 是否有任何通用尺寸

javascript - 如何在 REACT 中保持和循环数组中 HTML 元素的样式?

javascript - 输入值未定义

javascript - 动态更改的下拉菜单不刷新。 (雾巴兹)