所以我有一个简单的api,表示在localhost:3000 / api上运行。它显示了一些json数据
var express = require('express'),
bodyParser = require('body-parser'),
path = require('path'),
scraperjs = require('scraperjs');
var app = express();
var crun = 'http://www.example.com/';
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var router = express.Router();
router.get('/', function(req, res){
scraperjs.StaticScraper.create('http://www.example.com/link')
.scrape(function($) {
return $(".h2 a").map(function() {
return {
title: $(this).text(),
link: crun + $(this).attr('href')
}
}).get();
}, function(news) {
res.jsonp(news);
})
})
app.use('/api', router);
app.listen(3000);
console.log('alles oke');
现在我想用angularjs来获取它。 angularjs应用程序在端口1337上运行(如果这很重要)但是我得到了whoops错误但我不明白为什么
.controller('HomeController', function($scope, $http) {
var url = 'http://localhost:3000/api';
$http({
method: 'JSONP',
url: url
}).success(function(data, status, headers, config) {
$scope.news = data;
}).
error(function(data, status, headers, config) {
console.log('Whoops error');
});
我的错误是Refused to execute script from 'http://localhost:3000/api' because its MIME type ('application/json') is not executable, and strict MIME type checking is enabled.
FOUND FIX
我使用了一个叫做cors for node的模块,只是添加了
app.use(cors());