[英]Connection refused error post request at javascript file on node.js server
I'm getting.net::ERR_CONNECTION_REFUSED error when i open related page with javascript file.当我打开包含 javascript 文件的相关页面时,出现 .net::ERR_CONNECTION_REFUSED 错误。 I'm using node.js as a server and, i'm writing post requests in a javascript file for my static pages.
我正在使用 node.js 作为服务器,并且正在为我的 static 页面在 javascript 文件中编写发布请求。 service_provider.js is my javascript file for i use writing javascript functions for my static pages(html).
service_provider.js是我的 javascript 文件,我使用它为我的 static 页面 (html) 编写 javascript 函数。 node.js is my node.js server file.
node.js是我的 node.js 服务器文件。
service_provider.js service_provider.js
function getJourneyAnalize(
_fonkSuccess,
_fonkBefore,
_fonkError,
_deviceId,
_driverId,
_dateStart,
_dateEnd,
_timeStart,
_timeEnd,
_map,
_kmStart,
_kmEnd
) {
var _fromBody = {
device_id: _deviceId,
driver_id: _driverId,
date_start: _dateStart,
date_end: _dateEnd,
time_start: _timeStart,
time_end: _timeEnd,
map: _map,
km_start: _kmStart,
km_end: _kmEnd
};
$.ajax({
type: 'POST',
url: apiUrl + '/ReportFms/JourneyAnalize',
data: JSON.stringify(_fromBody),
dataType: 'json',
cache: false,
contentType: 'application/json; charset=utf-8',
beforeSend: function (xhr, settings) {
_fonkBefore();
xhr.setRequestHeader('Authorization', 'Bearer ' + currentUser.access_token);
},
success: _fonkSuccess,
error: _fonkError
});
}
node.js node.js
const express = require('express');
const app = express();
var path = require('path');
app.use(
function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
}
)
app.use('/node_modules/jquery/dist', express.static(path.join(__dirname, '../node_modules/jquery/dist')))
app.get('/static-pages/journey-analize-report', function (req, res) {
res.sendFile(path.join(__dirname + '/../static-pages/journey-analize-report/index.html'))
})
app.listen(8000, () => {
console.log('server started');
})
Does you static page shows correctly?你static页面显示正确吗?
1 - If your static page is in the parent folder, get rid of the first slash 1 - 如果您的 static 页面在父文件夹中,去掉第一个斜线
res.sendFile(path.join(__dirname + '../static-pages/journey-analize-report/index.html'))
2 - to serve jquery script in your static file you could define a personnalized path in place of node_module folder 2 - 要在 static 文件中提供 jquery 脚本,您可以定义一个个性化路径来代替 node_module 文件夹
Replace:代替:
app.use('/node_modules/jquery/dist', express.static(path.join(__dirname, '../node_modules/jquery/dist')))
With和
app.use('/js', express.static(path.join(__dirname, '../node_modules/jquery/dist')))
and in your html file require jquery with personnalized path在你的 html 文件中需要 jquery 和个性化路径
<script src="/js/jquery.min.js"></script>
3 - if the error still exist, comment app.use functions and jquery script in static page. 3 - 如果错误仍然存在,请在 static 页面中评论 app.use 函数和 jquery 脚本。 Reload the server to see if its work
重新加载服务器以查看其是否正常工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.