0

I've inserted the following lines into my code:

var express = require('express'); var cors = require('cors'); var app = express(); app.options('*', cors()); app.use(cors());

However, when I make an AJAX POST request, I still get the error:

POST https://stormy-plateau-94715.herokuapp.com/login new-page-5:1 XMLHttpRequest cannot load https://stormy-plateau-94715.herokuapp.com/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://squareguru.squarespace.com' is therefore not allowed access. The response had HTTP status code 503. 

This is my first time using the cors package, am I configuring something wrong? I would love any help you guys can give me, thanks!

10
  • Is stormy-plateau-94715 your app or someone else's Commented Jul 18, 2016 at 14:31
  • It's my app. It's one of the free hobby dynos on Heroku. Commented Jul 18, 2016 at 14:33
  • Try adding this code to the top of your login endpoint: res.setHeader("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "X-Requested-With"); Commented Jul 18, 2016 at 14:39
  • I added the following code: app.all('*', function(req, res, next){ res.writeHead(200, {'Content-Type': 'text/html'}); res.header("Access-Control-Allow-Origin", "*"); res.header('Access-Control-Allow-Methods', 'OPTIONS,GET,POST'); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); next(); }); I'm still getting the same error Commented Jul 18, 2016 at 14:41
  • 1
    I guess you don't need app.options('*', cors()); just app.use(cors()); Commented Jul 18, 2016 at 14:50

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.