繁体   English   中英

sails.js中的骨干网

[英]Backbone in sails.js

我正在创建一个使用帆和护照进行身份验证的站点。 我在代码中使用Jquery和骨干网时遇到了问题。 当我尝试将它们与帆一起使用时,似乎两者都下降了。 在用户身份验证之后,我想做的是将用户路由到存在所有脚本的主页。 我将所有.js文件放在layout.ejs中,例如:

<link rel="stylesheet" href="styles/bootstrap-theme.min.css">
<script type="text/javascript" src="js/jquery.js"></script>

大多数css和js文件都可以工作,但是我遇到了骨干网和jquery(和jquerymobile)的问题。 我正在使用jQuery 1.10.2和主干1.1.0。 知道有什么问题吗?

在骨干代码中,我试图通过php文件发出Ajax请求。

   var ProfileList = Backbone.Collection.extend({

                    model: ProfileModel,
                    url: 'data.php'
    });   

我到底要做什么? 要在路线中添加网址? 还是应该在哪里放置data.php文件?

编辑:我已经更改了gruntfile.js,将jquery放在顶部,并且工作正常。 现在问题仍然是骨干。 我猜是因为我要求使用护照和骨干网访问不同的域而引起麻烦。 当我使用data.php请求数据时,我在调用以下jquery代码:

xhr.send( ( s.hasContent && s.data ) || null ): jquery.js (line 8706)   

这是一个Ajax XMLHttpRequest发送请求http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp 我通常尝试从两个域localhost和localhost:1337请求数据,因此出现了跨域问题。 我发现cors(跨源资源共享)可以解决此问题。 任何想法如何允许在sails.js中使用cors?

编辑:在route.js文件中,我将主页cors设置为true:

'/secondscreen': {
view: 'secondsocialscreen/index',
cors: true  

}

我仍然收到相同的错误。 我也将config / cors.js文件中的变量AllRoutes更改为true。 我的主干文件有效(使用控制台检查),但是我无法获取数据。 Config / cors.js文件如下:

module.exports.cors = {

    // Allow CORS on all routes by default?  If not, you must enable CORS on a 
    // per-route basis by either adding a "cors" configuration object
    // to the route config, or setting "cors:true" in the route config to
    // use the default settings below.
    allRoutes: true,

    // Which domains which are allowed CORS access?
    // This can be a comma-delimited list of hosts (beginning with http:// or https://)
    // or "*" to allow all domains CORS access.
    origin: '*',

    // Allow cookies to be shared for CORS requests?
    credentials: true,

    // Which methods should be allowed for CORS requests?  This is only used
    // in response to preflight requests (see article linked above for more info)
    methods: 'GET, POST, PUT, DELETE, OPTIONS, HEAD',

    // Which headers should be allowed for CORS requests?  This is only used
    // in response to preflight requests.
    headers: 'content-type' 
    };

我错过了什么吗? 好吧,我想我已经找到了一些东西。 实际上,我试图将home.ejs定义为存在主干代码的fetching.js(从data.php获取数据和定义模型-控制器-视图)。 这个文件是否有可能按原样工作,还是我必须从帆中定义我的MVC? 另外我发现我正在尝试从localhost:1337(帆服务器)获取数据。 但是我想从apache localhost获取数据。 航行时如何向Apache服务器请求数据? 在萤火虫中,我收到了以上信息:

 GET http://localhost/sitec/fetchdata.php?widget=highlights 200 OK 47ms jquery.js (line 8706)
 GET http://localhost/sitec/fetchdata.php?widget=sentiment 200 OK 47ms jquery.js (line 8706)
 GET http://localhost/sitec/fetchdata.php?widget=tagsCloud 200 OK 47ms jquery.js (line 8706)

作为回应,我没有看到json数据的对象(data.php返回json文件),实际上我可以看到data.php文件的代码。 奇怪的

默认情况下,Sails.js使用grunt添加CSS和JS文件。 手动在布局中添加一些内容不是一个好主意。 在主根目录下的Gruntfile.js文件中,您将看到两个重要的数组-cssFilesToInject和jsFilesToInject。 要以正确的顺序加载文件,只需按需要在此处添加即可。

var jsFilesToInject = [

// Below, as a demonstration, you'll see the built-in dependencies 
// linked in the proper order order

// Bring in the socket.io client
'linker/js/socket.io.js',

// then beef it up with some convenience logic for talking to Sails.js
'linker/js/sails.io.js',

// A simpler boilerplate library for getting you up and running w/ an
// automatic listener for incoming messages from Socket.io.
'linker/js/app.js',

// *->    put other dependencies here   <-*
'path_to_jquery',
'path_to_underscore',
'path_to_backbone',

// All of the rest of your app scripts imported here
'linker/**/*.js'
];

我们通过修改config / routes.js在帆中使用CORS

module.exports.routes = {

'/ *':{cors:true}

}

此处记录: http : //sailsjs.org/#! documentation/ config.routes

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM