0

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)});
    }

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??

raksha
  • 11
  • 2

2 Answers2

0

you need to add this:

header('Access-Control-Allow-Headers: Content-Type');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Origin: *');

My project backend is laravel. So, I included this in Route file.

Mohammed
  • 648
  • 2
  • 12
  • 32
  • my backened is in expressJS. so whether i need to include these header files – raksha Jan 30 '17 at 13:17
  • visit this : http://stackoverflow.com/questions/11181546/how-to-enable-cross-origin-resource-sharing-cors-in-the-express-js-framework-o – Mohammed Jan 30 '17 at 13:19
0

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.

R. Gulbrandsen
  • 3,648
  • 1
  • 22
  • 35