简体   繁体   中英

XMLHttpRequest cannot load angular

I am new to Angular and am trying to build a simple app to teach myself. I don't understand why I am getting the error mentioned in the subject? "XMLHttpRequest cannot load localhost:8081/scrape. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource."

I navigate to the port I specified (8081), and yet I get a CORS error even though i enabled cors through express:

Angular Factory:

.factory('TradesDataFactory', function($http) {

        var getPlayerInfo = function() {
            return $http({
                method: 'GET',
                url: 'localhost:8081/scrape'
            }).then(function(resp) {
                return resp.data
            });
        };

express: scrape

var express = require('express');
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var cors = require('express-cors')
var app = express();

app.use(cors());
app.use(express.static('client'));
//*** how do I get this to scrape on server load?

app.get('/scrape', function(req, res) {

    //All the web scraping magic will happen here. do this once.
    url = "https://www.bing.com/search?q=Week+1+running%20backs+fantasy+predictions&FORM=MA12R5&OCID=MA12R5&wt.mc_id=MA12R5";

    request(url, function(error, response, html) {
        if (!error) {
            var $ = cheerio.load(html);

            var player, projectedPoints;
            var players = [];

            var playersAndPoints = $('.items').text();
            var playersAndPoints = JSON.stringify(playersAndPoints);
            var re = /Points\s\(PPR\)/gi;
            var formattedPlayers = playersAndPoints.replace(re, " ");
            formattedPlayers.replace("\\\"", "\"");
            var regex2 = /\.\d/g;
            var formattedPlayers = formattedPlayers.split(regex2);

            console.log(formattedPlayers);

            var names = formattedPlayers.map(function(player) {
                return player.split(':')[0]
            });
            var score = formattedPlayers.map(function(player) {
                return player.split(':')[1]
            });

            var object = {}

            for (var i = 0; i < names.length; i++) {
                object[names[i]] = score[i];
            }
        }

        var formattedPlayers = object;

        fs.writeFile('output.json', JSON.stringify(formattedPlayers, null, 4), function(err) {
            console.log("check your file directory!")
        })
        res.send("check your console!")
    })
})

app.listen('8081')

console.log('Magic happens on port 8081');

exports.app = app;

The error you are getting is Angular checking the protocol of the URL, like http , data or file .

If you change the url parameter to: url: 'http://localhost:8081/scrape' , it should work, making your $http method look like:

return $http({
    method: 'GET',
    url: 'http://localhost:8081/scrape'
}).then(function(resp) {
    return resp.data
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM