我有一个控制器(login.controller.js):
var express = require('express');
var router = express.Router();
var request = require('request');
var config = require('config.json');
router.get('/', function (req, res) {
// log vendor out
delete req.session.token;
// move success message into local variable so it only appears once (single read)
var viewData = { success: req.session.success };
delete req.session.success;
res.render('login', viewData);
});
router.post('/', function (req, res) {
// authenticate using api to maintain clean separation between layers
request.post({
url: config.apiUrl + '/vendors/authenticate',
form: req.body,
json: true
}, function (error, response, body) {
if (error) {
return res.render('login', { error: 'An error occurred' });
}
if (!body.token) {
return res.render('login', { error: body, vendorname: req.body.vendorname });
}
// save JWT token in the session to make it available to the angular app
req.session.token = body.token;
// redirect to returnUrl
var returnUrl = req.query.returnUrl && decodeURIComponent(req.query.returnUrl) || '/';
res.redirect(returnUrl);
});
});
module.exports = router;
var请求正在访问Remote REST Api。哪里有一个服务(vendor.service.js),其中$ http不是请求。但$ http无效:
(function () {
'use strict';
angular
.module('app')
.factory('VendorService', Service);
function Service($http, $q) {
var service = {};
//Store
service.GetCurrent = GetCurrent;//st_details
return service;
function GetCurrent() {
return $http('http://localhost:3000/api/vendors/st_details').then(handleSuccess, handleError);
}
function handleSuccess(res) {
alert('hi3');
alert(res.data);
return res.data;
}
function handleError(res) {
alert('hi2');
alert(JSON.stringify(res));
return $q.reject(res.data);
}
}
})();
我可以使用“var request = require('request');”在这个vendor.service.js中,如果是的话怎么样?提前感谢一百万!
答案 0 :(得分:0)
您似乎没有使用CORS,您需要使用它才能允许应用通过$ http.get
访问您的API按照https://www.npmjs.com/package/cors上的说明将CORS添加到您的快速应用程序中,它应该可以正常运行。