1、在前端请求ajax的时候会遇到跨域的问题,不能够请求到数据,需要搭建一下中间层,使用express来请求ajax数据然后前端再去请求后台返回的数据,这样就不会遇到跨域问题,express代码如下:
var express=require('express');
var router=express.Router();
var http=require('http');//引入http模块
//后台解决跨域问题
router.all('*',function(req,res,next) {
res.header("Access-Control-Allow-Origin","*");
res.header('Access-Control-Allow-Methods','PUT, GET, POST, DELETE, OPTIONS');
res.header("Access-Control-Allow-Headers","X-Requested-With");
res.header('Access-Control-Allow-Headers','Content-Type');
next();
});
// 轮播的接口
// localhost:3000/lunbo
router.get('/lunbo',function(req,res) {
varpage=req.query.page;
varcount=req.query.count;
// 要去请求 卖座网的接口
// http://m.maizuo.com/v4/api/billboard/home?__t=1500253189212
vartime=newDate().getTime();
http.get('http://m.maizuo.com/v4/api/billbo