【问题标题】:JQuery: AJAX Not Showing Result From ServerJQuery:AJAX 不显示来自服务器的结果
【发布时间】:2018-03-17 20:25:03
【问题描述】:

我正在使用 jQuery 来执行 Ajax,然后构造一个 JavaScript object 将被发送回浏览器。但是,服务器发送数据后,数据并没有显示在浏览器中。代码如下:

jQuery

$.ajax({
      url: "/home",
      type: "POST",
      contentType: "application/json",
      data: JSON.stringify(//sent data...)
  }).done(function(result){
      console.log(result.html);
      console.log(result.number);
  })

ExpressJS

router.post("/home", function(req, res){
    User.findById(id).exec(function(err, user){
        res.render("home", {user: user}, function(err, html){
            res.send({html: html, number: 123});
            console.log({html: html, number: 123});
        }
    });
});

在 Express 代码中,“html”是一堆呈现的 html 代码。当我在 Express 中console.log 时,终端会显示预期的数据。但是,在浏览器端,console.log(result.html)console.log(result.number) 都没有在浏览器的控制台中显示任何内容。我不太确定哪里出错了。

【问题讨论】:

  • 从 expressJs 代码中删除 console.log({html: html, number: 123}); 并在“完成”函数中添加“错误”参数:function(err, result) { console.log(result.html); console.log(result.number); }
  • 是的,我已经做到了。为了便于理解,这里对代码进行了编辑。

标签: jquery express


【解决方案1】:

目前无法对其进行测试,但我认为您应该在“完成”函数中添加一个“错误”参数:

function(err, result) { 
    console.log(result.html);
    console.log(result.number);
}

【讨论】:

  • 是的,我已经做到了。这里对代码进行了修改,以便于理解。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多