Guest User

Untitled

a guest
Jan 23rd, 2019
1,080
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. Access to fetch at [URL] from origin [URL] has been blocked by CORS Policy.
  2. No 'Access-Control-Allow-Origin' header is present on the requested resource.
  3.  
  4. const cors = require('cors')({ origin: true });
  5.  
  6. app.get('/my-route', (req, res) => {
  7. return cors(req, res, () => {
  8. res.render('view-report-template', context, (err, html) => {
  9. res.status(200).send(html);
  10. });
  11. });
  12.  
  13. const cors = require('cors')({ origin: true });
  14.  
  15. app.get('/my-route', (req, res) => {
  16. res.render('view-report-template', context, (err, html) => {
  17. res.status(200).send(html);
  18. });
  19. });
  20.  
  21. app.use(cors);
  22.  
  23. app.use((req, res, next) => {
  24. res.header("Access-Control-Allow-Origin", "*");
  25. res.header('Access-Control-Allow-Methods', 'DELETE, PUT, GET, POST');
  26. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With,
  27. Content-Type, Accept");
  28. next();
  29. });
  30.  
  31. app.get('/my-route', (req, res) => {
  32. res.render('view-report-template', context, (err, html) => {
  33. res.status(200).send(html);
  34. });
  35. });
  36.  
  37. app.use((req, res, next) => {
  38. res.header("Access-Control-Allow-Origin", "*")
  39. res.header(
  40. "Access-Control-Allow-Headers",
  41. "Origin, X-Requested-With, Content-Type, Accept, Authorization"
  42. );
  43. if(req.method === 'OPTIONS'){
  44. res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH,
  45. DELETE, GET');
  46. return res.status(200).json({});
  47. }
  48. next();
  49. });
  50.  
  51. app.get('/my-route', (req, res) => {
  52. res.render('view-report-template', context, (err, html) => {
  53. res.status(200).send(html);
  54. });
  55. });
  56.  
  57. app.get('/my-route', (req, res) => {
  58. res.set('Access-Control-Allow-Origin', '*');
  59. res.status(200).send(html);
  60. });
  61.  
  62. fetch('https://us-central1-[project-id].cloudfunctions.net/my-route', {
  63. method: "GET",
  64. mode: "cors"
  65. })
  66. .then(res => res.text())
  67. .then(data => console.log(data));
Add Comment
Please, Sign In to add comment