“Access-Control-Allow-Origin Nodejs Express” Réponses codées

Access-Control-Allow-Origin Nodejs Express

// Add headers
app.use(function (req, res, next) {

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

    // 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();
});
Precious Plover

Autoriser le nœud d'origine croisée

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});
Magnificent Manatee

Access-Control-Allow-Origin Nodejs Express


var express = require('express');
var app = express();
var Assessment = require('../app/models/assessment');

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

app.post('/api/status', function (req, res, next) {
    // your code goes here
});

module.exports = app;

Clear Crocodile

express js cors

var express = require('express')
var cors = require('cors')  //use this
var app = express()

app.use(cors()) //and this

app.get('/user/:id', function (req, res, next) {
  res.json({user: 'CORS enabled'})
})

app.listen(5000, function () {
  console.log('CORS-enabled web server listening on port 5000')
})
Batman

Ajouter l'origine accès au contrôle-allow dans le nœud js

// Install cors package: npm install cors
const express = require("express");
const app = express();
const cors = require('cors');
app.use(cors());
// your http request should work after this
Just Make It Global

Expression CORS Policy Non 'Access-Control-Allow-Origin'

$ npm install cors
Condemned Crane

Réponses similaires à “Access-Control-Allow-Origin Nodejs Express”

Questions similaires à “Access-Control-Allow-Origin Nodejs Express”

Plus de réponses similaires à “Access-Control-Allow-Origin Nodejs Express” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code