我正在尝试将3D模型加载到Three.js中JSONLoader,并且该3D模型与整个网站位于同一目录中.
我收到了"Cross origin requests are only supported for HTTP."错误,但我不知道是什么导致它,也不知道如何解决它.
基本上,我需要运行带有json-server的Node / Express服务器,以模拟用于正在开发的测试Angular应用的RESTFUL API。问题是Google Chrome浏览器抛出错误,表明我遇到了CORS问题。我尝试以解决CORS问题的方式配置服务器。。。
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
Run Code Online (Sandbox Code Playgroud)
。。。无济于事。
如果不摆弄JSONP或以任何方式更改Angular代码,我还能做更多的事情吗?在Rails服务器上似乎工作正常。
编辑:正如许多人所建议的那样,我尝试使用cors模块来解决该问题,但这似乎也不起作用。这是我的server.js文件:
var express = require('express');
var app = express();
var path = require("path");
var jsonServer = require("json-server");
var databaseServer = jsonServer.create();
var cors = require("cors");
app.use(cors());
app.use("/", express.static(__dirname));
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
var object = {
"fighters": [
{
"id": 1, …Run Code Online (Sandbox Code Playgroud) 我正在玩基本的Angular JS.我以为我很了解自定义指令但是如果我将它们用作元素属性,我只能让它们出现在模板中.而且我不想使用评论或类名,因为那些不是最佳实践.基于文档和其他来源,这些指令应该作为元素和元素属性.
在我index看来,我会看到我两次声明我的指令 - 在顶部块作为属性(工作)和底部块作为元素(不起作用).我有几个小时深入研究这个问题.非常感谢任何见解.
的index.html
<head>
<title>Test App</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href='styles/css/style.css' rel='stylesheet' type="text/css">
<script src="vendor/angular_1.2.13/angular.min.js"></script>
</head>
<body ng-app="testApp" class="app__body">
<div class="body__inner">
<header>
<h1>My New Test App</h1>
</header>
<div first-directive></div>
<div second-directive></div>
<first-directive></first-directive>
<second-directive></second-directive>
</div>
<script src="scripts/all-in-one.js" type="text/javascript"></script>
</body>
Run Code Online (Sandbox Code Playgroud)
所有功能于one.js
'use strict';
angular.module('testApp', [])
.directive('firstDirective', function() {
return {
template: 'Hello World'
}
})
.directive('secondDirective', function() {
return {
templateUrl: '/scripts/directives/secondDirective.js'
}
})
.controller('firstCtrl', function($scope) {
$scope.message = "Hola Amigos!"
})
Run Code Online (Sandbox Code Playgroud)
secondDirective.js
<div>
<h2>Esta Aqui!</h2> …Run Code Online (Sandbox Code Playgroud)