我有一个foo发出Ajax请求的函数.我怎样才能从中回复foo?
我尝试从success回调中返回值,并将响应分配给函数内部的局部变量并返回该变量,但这些方法都没有实际返回响应.
function foo() {
    var result;
    $.ajax({
        url: '...',
        success: function(response) {
            result = response;
            // return response; // <- I tried that one as well
        }
    });
    return result;
}
var result = foo(); // It always ends up being `undefined`.
假设对于来自API的每个响应,我需要将响应中的值映射到我的Web应用程序中的现有json文件,并显示json中的值.在这种情况下,读取json文件的更好方法是什么?require或fs.readfile.请注意,可能会有数千个请求同时进入.
请注意,我不认为在运行时期间文件有任何更改.
request(options, function(error, response, body) {
   // compare response identifier value with json file in node
   // if identifier value exist in the json file
   // return the corresponding value in json file instead
});
我正在构建一个使用vs代码扩展来解析json的扩展.所以我的需要是,它应该能够从特定文件夹加载.json文件并迭代文件的内容.然后它应该允许用户从中选择几个键,从中创建一个新的json文件,并将其保存在任何文件夹中.
但我无法找到任何方式来读取和写入"vs代码扩展"中的文件.有人可以帮助我.
我已经看到了在Nodejs中从localy读取JSON文件的不同方法.像这样;
方法
使用fs库
同步
var fs = require('fs');
var obj = JSON.parse(fs.readFileSync('file', 'utf8'));
异步:
var fs = require('fs');
var obj;
fs.readFile('file', 'utf8', function (err, data) {
  if (err) throw err;
  obj = JSON.parse(data);
});
方法
使用require()
let data = require('/path/file.json');
方法
使用Ajax请求 如何使用Jquery和ajax从JSON文件中检索数据?
可能还有其他方法.但我听说使用方法1读取JSON文件比其他方法更有效.
我正在开发一个模块,我必须在每个客户端请求时读取一个JSON文件,而我当前正在使用方法1.这是银行应用程序,性能很重要.所以帮助我找到哪种方式使用这个senario好?
谢谢,任何帮助将不胜感激!
我正在尝试获取这件作品中数组中的项目数
JSON
{
  "collection" : [
    {
      "item": "apple"
    },
    {
      "item": "banana"
    }]
}
使用以下JS (NodeJS): 使用用户“elssar”的答案更新
var data = JSON.parse(fs.readFileSync(filePath));
console.log(data.collection.length);
预期结果:2
没有指定编码data将是一个缓冲区而不是字符串(感谢用户 nils)。JSON.parse 应该适用于两者。现在我收到一个错误Unexpected token ? at Object.parse (native)。知道如何解决这个问题吗?(使用节点 5.2.0)
我想从文件中读取数据并将其添加到存储在内存中的对象中。文件 text.txt 中的数据大致如下所示:
One: {title: 'One' ,
contributor: 'Fred',
summary: 'blah' ,
comments: 'words' },
Two: {title: 'Two' ,
contributor: 'Chris' ,
summary: 'blah blah i'm a blah' ,
comments: '' },
我试图将其设置为一个空对象,如下所示:
var fs = require('fs');
var text = Object.create(null);
fs.readFile("./public/text.txt", "utf-8", function(error, data) {
  text = { data };
});
然而,当我登录text到控制台时,它看起来像这样:
{ data: 'One: {title: \'One\' ,\ncontributor: \'Fred\',\nsummary: \'blah\' ,\ncomments: \'words\' },\n    \nTwo: {title: \'Two\' ,\ncontributor: \'Chris\' ,\nsummary: \'blah blah i\'m a blah\' ,\ncomments: \'\' },\n\n' …我想用express来向本地JSON文件发出GET请求.
在我的server.js中我有这个
var data = {};
app.get('/src/assets/data.json', (req, res) => {
  console.log(res)
  res.writeHead(200, {
    'Content-type': 'application/json'
  });
  res.end(JSON.stringify(data));
});
data.json看起来像这样
[{
    "param": "one",
    "param": "two",
    "param": "three"
  }]
我还为GET请求创建了一个函数,只要加载DOM就会调用它
getData() {
    let xhr = new XMLHttpRequest();
    xhr.open('GET', '/src/assets/data.json', true);
    xhr.onreadystatechange = () => {
      if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
        console.log(xhr)
      }
    };
    xhr.send();
  }
我收到了回复,但它是一个空的对象.我猜这是因为我的服务器文件var data = {};是空的但我不知道该怎么办呢?
我是 Node.js 的新手,有点被困在这里。我有一个类似于此的 json 文件 keyValue.json
[ {
    "key": "key1",
    "value": "value1"
  },   
  {
    "key": "key2",
    "value": "value2"
  } 
]
对于特定的键,我需要获取其值。
function filterValue() {
  const category = {};
  category.filter = "key1" //this is not static value. This changes dynamically
  category.value = //Need to read the keyValue.json file and pick the value for key1 - which is value1
  return category;
}
在node.js 中如何实现这一点?感谢您的帮助。
javascript ×6
node.js ×6
json ×4
ajax ×2
express ×2
object ×2
asynchronous ×1
fs ×1
jquery ×1
readfile ×1