关于跨域的实现

今天在做公司的项目时,有一个公司的智慧公园的大屏管理,就是用到一个gulp的代理实现了跨域.

  1. 什么是跨域,就是你的项目运行在www.xxx.com,而你的请求也是www.xxx.com,这样才不需要进行跨域.否则,都是要进行跨域的.
  2. ajax的跨域实现就是配置一个dataType:”jsonp”;
    gulp实现跨域的代码如下:当然,这这是百度的.

  3. 以下代码写在gulpfile.js文件中就行了

    //引入插件
    var gulp = require('gulp');
    // var Proxy = require('gulp-connect-proxy');
    var connect = require('gulp-connect');
    var proxy = require('http-proxy-middleware');
    
    //使用connect启动一个Web服务器
    gulp.task('connect', function () {
        connect.server({
            root: './',
            livereload: true,
            port: 8010,
            middleware: function (connect, opt) {
                return [
                    proxy('/fs', {
                        target: 'http://172.16.2.52:8080',
                        changeOrigin:true
                    }),
                    proxy('/product', {
                        target: 'http://172.16.1.60:8080',
                        changeOrigin:true
                    }),
                    proxy('/bpauth', {
                        target: 'http://192.168.24.77:8080',
                        changeOrigin:true
                    })
                ]
            }
        });
    });
    
    gulp.task('watch', function () {
        gulp.watch(['./*.html'], ['html']);
    });
    
    gulp.task('html', function () {
        gulp.src('./*.html')
            .pipe(connect.reload());
    });
    
    //运行Gulp时,默认的Task
    gulp.task('default', ['connect', 'watch']);

猜你喜欢

转载自blog.csdn.net/stephen__wu/article/details/79417674