Angular js - Cross-Request Request Blocked in GET Request

I have a list of a pair of key URL values ​​and I access them in a function with a name accessUrl. The URLs we hit are external URLs and are not supported by us, that is, I cannot change the server code that sends us a response from these URLs

http.get() success gives me status and response.

var accessUrl = function(){
  $.each(url, function(key, value)
  {
      $http.get(value).success(function(response,status){

      $scope.status=status;
      if($scope.status == "200"){
           versionMap[key]=response.version;
           setColor[key]='color-green';
           //console.log("map "+versionMap[key]);
        }
    })//end of success

       .error(function(err){
          versionMap[key]="Down";
          setColor[key]='color-red';
        })//end of error
  })//end of each loop
}//end of accessUrl

I am trying to make a cross search request. Chrome has now added the CORS extension.

My app.js:

var express = require("express");
var app     = express();
var path    = require("path");

app.use("/", express.static(__dirname));

app.get('/',function(req,res){
  res.sendFile(path.join(__dirname+'/index.htm'));
  //__dirname : It will resolve to your project folder.
});

app.set('port', process.env.PORT);
console.log(port);

I no longer want to use the CORS extension. Without the extension, I encountered the following error:

: https://myurl.com (: CORS "Access-Control-Allow-Origin" ).

, Angular, CORS ?

, :

, , .

+4
4

node js-

var express = require("express");
 var app = express();
 var path = require("path");

 app.use("/", express.static(__dirname));

 app.get('/', function(req, res) {
     res.sendFile(path.join(__dirname + '/index.htm'));
     //__dirname : It will resolve to your project folder.
 });

 app.use(function(req, res, next) {

     // Website you wish to allow to connect
     res.setHeader('Access-Control-Allow-Origin', '*');

     // Request methods you wish to allow
     res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

     // Request headers you wish to allow
     res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

     // Set to true if you need the website to include cookies in the requests sent
     // to the API (e.g. in case you use sessions)
     res.setHeader('Access-Control-Allow-Credentials', true);

     // Pass to next layer of middleware
     next();
 });

 app.set('port', process.env.PORT);
 console.log(port);
0

httpProvider , app.js

$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';

.

0

cors , / - GET/POST, 2 : 1:

var cors = require('cors');
app.use(cors());

npm:

npm install cors --save

Method2:

, :

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'DELETE, PUT', 'GET', 'POST');
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

3:

, cors chrome extension, . P.S: , google/youtube ..

0

API , API. API, API. . - API .

This can be done using the header "Access-Control-Allow-Origin". Your external API should send a header with the parameter "Access-Control-Allow-Origin" as the key, and your host name as the value. There is no other way around this.

0
source

Source: https://habr.com/ru/post/1674951/


All Articles