So i have a simple api with express thats running on localhost:3000/api . It shows some json data
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');
And now i am trying to fetch it with angularjs. The angularjs app runs on port 1337 (if that matters) but i am getting the whoops error but i don't understand why
.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');
});
My error is 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
i used a module called cors for node and just added
app.use(cors());
JSONP
toGET
or change Node tores.jsonp(news);
res.jsonp(news);
and change Angular fromJSONP
toGET