本文介绍了跨源请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从前端备份上传图像....我将使用ngFileUploader bower Component.我的前端代码是:

I want to Upload Image in backened from frontend....I'll use ngFileUploader bower Component.My frontend code is:

function SampleController(SampleData,Upload,$http) {
      var vm = this;
      vm.uploadFiles = function(files, errFiles) {
        Upload.upload({
      url: "localhost:5000/upload", //webAPI exposed to upload the file
      data: {
        file: files
      }
    }).then(function(resp) {
      console.log(resp)});
    }

然后我将在其html文件中添加ngf-select.它会显示错误-XMLHttpRequest无法加载localhost:5000/upload.跨源请求仅支持以下协议方案:http,数据,chrome,chrome扩展名,https,chrome-extension-resource.我该如何解决?

And i'll added ngf-select in its html file. And it will show the error--XMLHttpRequest cannot load localhost:5000/upload. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource. How can I resolved it??

推荐答案

将cors过滤器作为中间件添加到您的应用程序中

Add cors filter to your application as a middleware

var app = require('express')();
app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
  res.header('Access-Control-Allow-Methods', ['GET', 'PUT', 'POST', 'DELETE']);
  next();
});

我建议您将允许的来源列入白名单.

I would recommend you white list the origins you would allow.

这篇关于跨源请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 22:54