【发布时间】:2015-06-26 02:02:39
【问题描述】:
我已经构建了一个基于 Backbone.js 的应用程序并将其部署到链接上的 heroku(https://prototypeapp.herokuapp.com),但我遇到了一个奇怪的问题: 当我在浏览器中请求应用程序时,与backbone.js 相关的代码部分不起作用,并且我的Firefox 浏览器的Web 控制台显示以下错误:“跨域请求被阻止:同源策略不允许读取https://localhost:8080/timeline 的远程资源。(原因:CORS 请求失败)。尽管该应用程序在本地运行良好。 我已经阅读过这个问题,但没有任何帮助我解决它。谁能帮我知道这个问题的原因以及如何解决它? 我的 server.js 代码如下:
/**
* A simple API hosted under localhost:8080
*/
var express = require('express');
var app = express();
var Twit = require('twit')
var client = null;
function connectToTwitter(){
client = new Twit({
consumer_key: '*******'
, consumer_secret: '*******'
, access_token: '*******'
, access_token_secret: '*******'
});
}
//get the app to connect to twitter.
connectToTwitter();
app.set('port', (process.env.PORT || 8080));
/**
* Get the account settings for the user with the id provided.
**/
app.get('/profile/:id', function(request, response){
response.header('Access-Control-Allow-Origin', '*');
client.get('users/show', {screen_name:request.params.id},
function (err, reply) {
if(err){
console.log('Error: ' + err);
response.send(404);
}
if(reply){
// console.log('Reply: ' + reply);
response.json(reply);
}
});
});
/**
* Runs a search given a query
**/
app.get('/search/:query', function (request, response) {
response.header('Access-Control-Allow-Origin', '*');
//search term is
var searchTerm = request.params.query;
client.get('search/tweets', { q: searchTerm, count: 20 },
function(err, reply) {
if(err){
console.log('Error: ' + err);
response.send(404);
}
if(reply){
// console.log('Reply: ' + reply);
response.json(reply);
}
});
});
/**
* Returns the twitter timeline for the current user
**/
app.get('/timeline', function (request, response) {
response.header('Access-Control-Allow-Origin', '*');
client.get('statuses/home_timeline', { count:6 },
function (err, reply) {
if(err){
console.log('Error: ' + err);
response.send(404);
}
if(reply){
// console.log('Reply: ' + reply);
response.json(reply);
}
});
});
var allowCrossDomain = function(req, response, next) {
response.header('Access-Control-Allow-Origin', "*");
response.header('Access-Control-Allow-Methods',
'OPTIONS, GET,PUT,POST,DELETE');
response.header('Access-Control-Allow-Headers', 'Content-Type,
Authorization, Content-Length, X-Requested-With');
if ('OPTIONS' == req.method) {
response.send(200);
}
else {
next();
}
};
app.configure(function() {
app.use(allowCrossDomain);
//Parses the JSON object given in the body request
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static('client'));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
//Start server
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
我的时间线集合如下:
define(['backbone', 'app/model/Tweet'], function(Backbone, Tweet) {
var com = com || {};
com.apress = com.apress || {};
com.apress.collection = com.apress.collection || {};
com.apress.collection.Timeline = Backbone.Collection.extend({
//the model that this collection uses
model: Tweet,
//the server side url to connect to for the collection
url: 'https://localhost:8080/timeline',
initialize: function(options){
//anything to be defined on construction goes here
},
});
return com.apress.collection.Timeline;
});
【问题讨论】:
标签: node.js backbone.js heroku