1. 程式人生 > >[Express] 如何允許Ajax跨域呼叫API

[Express] 如何允許Ajax跨域呼叫API

Express的api介面允許Ajax跨域呼叫API

var express = require('express');
var router = express.Router();

router.all('*',function (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"
,"PUT,POST,GET,DELETE,OPTIONS"); next(); });