jQuery AJAX到节点服务器抛出net :: ERR_CONNECTION_REFUSED [英] jQuery AJAX to node server throws net::ERR_CONNECTION_REFUSED

查看:3799
本文介绍了jQuery AJAX到节点服务器抛出net :: ERR_CONNECTION_REFUSED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已禁用防火墙(ufw在Ubuntu 15.04服务器上)。这似乎是导致此问题的最常见原因。

I have disabled my firewall (ufw on Ubuntu 15.04 server). This seems to be the most common cause of this problem.

此外,有些人在使用AJAX时遇到问题,所以我尝试了ajax和https POST请求。我的代码如下:

Additionally some people have problems using AJAX, so I tried with both ajax and an https POST request. My code is as follows:

jQuery发出请求:

jQuery making the request:

testData = {
    email     : address,
    subject   : subject,
    body      : body,
    meetingId : meetingId
};

testData = JSON.stringify(testData);

$.ajax({
    url: "https://localhost:3000/sendICSInvite",
    contentType: "application/json; charset=utf-8",
    data: testData,
    type: "POST",
    success: function(result) {
        console.log("request might have worked");
        console.log(result);
    },
    error: function (err) {
        console.log(err);
    }
}); 

到路线:

var api = require("../controllers/main.js");

//API ROUTES
module.exports = function(app){
  app.post('/sendICSInvite', api.sendEmail);
};

管制员:

module.exports.sendEmail = function (req, res) {
  console.log("sending Email (probably not actually)");
});

这可以通过邮递员调用并且完美无缺。但是当我从jQuery调用时,它会失败并返回错误:

This can be called with postman and works flawlessly. When I do a call from jQuery, however, it fails and returns the error:

OPTIONS https://localhost:3000/sendICSInvite?email=myemail%40myprovider.com&subject=TE…+emails&body=Just+testing+the+function+for+autmoated+email&meetingId=33033 net::ERR_CONNECTION_REFUSEDsend 
@ jquery.js:4m.extend.ajax
@ jquery.js:4sendEmail 
@ caseman.js:706(anonymous function) 
@ VM4606:2InjectedScript._evaluateOn 
@ VM4491:875InjectedScript._evaluateAndWrap 
@ VM4491:808InjectedScript.evaluate 
@ VM4491:664

似乎与OPTIONS请求(我不知道发送到哪里,我在我的ajax中输入POST类型),这导致我在我的节点服务器中提出这个建议的解决方案:

Seems to be related to the OPTIONS request (I don't know where this is sent, I sepcifiy POST type in my ajax), which lead me to this proposed solution in my node server:

app.use(function(req, res, next) {  
  res.header('Access-Control-Allow-Origin', "*");  
  res.header('Access-Control-Allow-Methods',"GET,PUT,POST,DELETE,OPTIONS");
  res.header('Access-Control-Allow-Headers', "*");  
  next(); 
});

但即使使用此中间件功能,错误也保持不变。节点服务器使用VMware Workstation在虚拟机上的端口3000上运行,并运行Ubuntu 15.04服务器版。包含我想发出此请求的jQuery的网站是通过端口80上的Perl脚本提供的。

But even with this middleware function the error stays the same. The node server is running on port 3000 on a virtual box using VMware Workstation and running Ubuntu 15.04 server edition. The website containing the jQuery that I want to issue this request is served through a Perl script on port 80.

推荐答案

数据:JSON.stringify(testdata),

这可能是你的问题。您需要序列化您的json对象。你需要在选项中将.ajax调用设置为POST类型,否则它将默认为get。

That's probably your problem. You need to serialize your json object. And you need to set the .ajax call to type of POST in options otherwise it will default to a get.

这篇关于jQuery AJAX到节点服务器抛出net :: ERR_CONNECTION_REFUSED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆