首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >sails.js骨干网

sails.js骨干网
EN

Stack Overflow用户
提问于 2013-11-26 08:36:16
回答 2查看 2.4K关注 0票数 2

我正在创建一个网站使用帆和护照的认证目的。但是,当涉及到Jquery和主干在代码中的使用时,我遇到了一些问题。当我试着用它们做帆的时候,两个人似乎都很沮丧。在用户身份验证之后,我将用户路由到所有脚本都存在的主页。我将所有.js文件放在layout.ejs中,用于exapmle:

代码语言:javascript
复制
<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请求.

代码语言:javascript
复制
   var ProfileList = Backbone.Collection.extend({

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

我到底要做什么?在线路中添加url?或者我应该把data.php文件放在哪里?

编辑:我已经更改了将jquery放在顶部的gruntfile.js,并且运行良好。现在,问题仍然是核心问题。我猜想我的麻烦出现了,因为我要求使用护照和主干访问不同的域名。当我使用data.php请求数据时,我调用以下jquery代码:

代码语言:javascript
复制
xhr.send( ( s.hasContent && s.data ) || null ): jquery.js (line 8706)   

这是一个Ajax XMLHttpRequest发送请求send.asp。我通常尝试从两个域( localhost和localhost:1337 )请求数据,因此出现了跨域问题。我发现cors(跨源资源共享)可以处理这个问题。知道如何在sails.js中允许cors吗??

编辑:在route.js文件中,我将主页cors交给true:

代码语言:javascript
复制
'/secondscreen': {
view: 'secondsocialscreen/index',
cors: true  

}

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

代码语言:javascript
复制
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获取数据并定义模型控制器视图),其中存在骨干代码。这个文件是否有可能按原样工作,还是我要从sails中定义MVC?我还发现,我正在尝试从localhost:1337 (sails服务器)获取数据。但是,我想从获取数据。如何在运行sails时向apache服务器请求数据?在firebug中,我收到了以下内容:

代码语言:javascript
复制
 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文件的代码。怪异

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-26 13:52:05

我们在sails中使用CORS,方法是修改config/scripes.js

module.exports.routes ={

'/*':{ cors:真}

}

这里有文档:http://sailsjs.org/#!documentation/config.routes

票数 3
EN

Stack Overflow用户

发布于 2013-11-27 10:30:34

默认情况下,Sails.js使用grunt添加CSS和JS文件。手工添加一些东西到布局中不是个好主意。在主根中的Gruntfile.js文件中,您将看到两个重要的数组-- cssFilesToInject和jsFilesToInject。要按正确的顺序加载文件,只需在这里添加它们以满足您的需要。

代码语言:javascript
复制
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'
];
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20211981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档