jQuery $ .get()不工作,返回状态200没有错误,但在客户端没有响应

时间:2015-05-07 02:36:03

标签: javascript jquery node.js express

我正在尝试向服务器发送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");
});

1 个答案:

答案 0 :(得分:0)

如果您已经访问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.