嗨,我一直面临着铬的问题。我不知道这是我的代码错误还是铬错误。错误是
无法加载资源:net :: ERR_CONNECTION_CLOSED
我的 QSTNURL 是
const app = angular.module('myapp',["ngRoute"]);
app.value("QSTNURL","https://localhost:12345/api/insertQuestion");
来自客户端的我的controller.js
app.controller('myctrl',function($scope,userFactory,QSTNURL){
$scope.pushData = function(){
var qstn = $scope.qstn;
var opt1 = $scope.opt1;
var opt2 = $scope.opt2;
var opt3 = $scope.opt3;
var opt4 = $scope.opt4;
var ans = $scope.ans;
const qstnObject = {
"qstn":qstn,
"opt1":opt1,
"opt2":opt2,
"opt3":opt3,
"opt4":opt4,
"ans":ans
}
console.log('doAjax');
var promise = userFactory.doAjax(QSTNURL,qstnObject);
promise.then(function(response){
$scope.data = response;
},function(error){
$scope.error = error;
});
}
});
我对服务器的ajax调用是
app.factory("userFactory",function($http,$q){
var userObject = {
doAjax(QSTNURL,qstnObject){
console.log('doAjax()');
var defer = $q.defer();
$http.get(QSTNURL,qstnObject).then(function(response){
defer.resolve(response);
console.log("doAjax 2");
},function(error){
defer.reject(error);
});
return defer.promise;
}
};
return userObject;
});
server.js
const express = require("express");
var expressSession = require('express-session');
const app = express();
var path = require("path");
var bodyParser = require('body-parser');
const userRoutes = require("./routes/userroutes");
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));
app.use(expressSession({secret: 'mySecretKey'}));
app.use('/api',userRoutes);
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/',userRoutes);
app.listen(12345,()=>{
console.log("Server Start...");
});
我的所有静态文件(例如: - 制作SPA的所有js文件,例如app.js,controller,facotry等)都在公共文件夹中并查看视图文件夹中的部分
我的路线代码是
router.get('/insertQuestion',(req,response)=>{
console.log("insertQuestion");
var qstn = req.body.qstn;
var opt1 = req.body.opt1;
var opt2 = req.body.opt2;
var opt3 = req.body.opt3;
var opt4 = req.body.opt4;
var ans = req.body.ans;
var qstnObject = new Insert(qstn,opt1,opt2,opt3,opt4,ans);
userOperation.insertQuestion(qstnObject,response,req);
response.json("question inserted");
});
答案 0 :(得分:0)
只有在12345端口上没有运行服务器时才会出现此错误。
您正在呼叫https://localhost:12345/dashboard/insertQuestion
。因此,您还应该考虑使用https
方案,因此请确保您的服务器支持https方案并且端口正确。
如果您尚未将节点应用/服务器应用配置为在https上运行,则只需替换
https://localhost:12345/dashboard/insertQuestion
与
http://localhost:12345/dashboard/insertQuestion
答案 1 :(得分:0)
https://localhost:12345/api/insertQuestion
,而只是将api/insertQuestion
直接传递给AJAX这样的调用
app.factory("userFactory",function($http,$q){
var userObject = {
doAjax(QSTNURL,qstnObject){
console.log('doAjax()');
var defer = $q.defer();
$http.get('/api/insertQuestion',qstnObject).then(function(response){
defer.resolve(response);
console.log("doAjax 2");
},function(error){
defer.reject(error);
});
return defer.promise;
}
};
return userObject;
});
我不知道为什么会这样。但它删除了我的问题