0

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());
10
  • Did you try to open localhost:3000/api in a browser? Commented Jul 22, 2015 at 7:51
  • why is your $http({ method: 'JSONP') ? aren't you trying to use the GET method to fetch the data? Commented Jul 22, 2015 at 7:52
  • @Michelem yes and it works Commented Jul 22, 2015 at 7:55
  • So change JSONP to GET or change Node to res.jsonp(news); Commented Jul 22, 2015 at 7:56
  • 2
    Change Node to res.jsonp(news); and change Angular from JSONP to GET Commented Jul 22, 2015 at 8:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.