【问题标题】:jQuery $.get() not working, returns status 200 without errors, yet no response on client sidejQuery $.get() 不起作用,返回状态 200 没有错误,但客户端没有响应
【发布时间】:2015-05-07 11:38:04
【问题描述】:

我正在尝试向服务器发送GET 请求以获取消息。我使用了jQuery$.get()方法,成功到达服务器。

但是,我无法将res 发送回客户端。我做错了什么?

// client side:
$.get("localhost:3000/load", function (data) {
    console.log("message from sever");  // is not logged
    console.log(data);                  // also is not logged
});

// server side:
app.get("/load", function (req, res) {
    console.log("Am I arrived in server?");  // appears in console
    res.end("hey");
});

【问题讨论】:

  • 尝试在客户端定位http://localhost:3000/load
  • 你在开发者工具的网络标签中得到了什么?
  • 我试过 http//: 但没有变化
  • 尝试在 res.end 上添加回调
  • 每次我点击按钮,我都得到 200 ok

标签: javascript jquery node.js express


【解决方案1】:

如果您已经在访问 localhost:3000,则将其从请求中删除...

$.get("/load", function (data) {
    console.log("message from sever");  // is not logged
    console.log(data);                  // also is not logged
});

...如果您没有访问 localhost:3000,则需要添加 http://

$.get("http://localhost:3000/load", function (data) {
    console.log("message from sever");  // is not logged
    console.log(data);                  // also is not logged
});

否则您将收到以下错误(在 Google Chrome 中)...

XMLHttpRequest cannot load localhost:3000/load. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

【讨论】:

  • 你能添加包装$.get的代码吗?这对我在客户端和服务器上都有效。
猜你喜欢
  • 2019-04-09
  • 2015-09-30
  • 1970-01-01
  • 1970-01-01
  • 2011-05-16
  • 1970-01-01
  • 2021-10-13
  • 2020-08-24
  • 2018-11-23
相关资源
最近更新 更多