vue 2.0用vue+webpack+vue-resource搭建的环境 在本地运行this.$http.post()时报错?

图片描述

vue 2.0中 用vue+webpack+vue-resource搭建的环境 在本地运行this.$http.post()....时报错?

阅读 3.3k
3 个回答

殊不知有反向代理这一说么,如果是脚手架,就配置config/index.js

dev: {
        env: require('./dev.env'),
        port: 9080,
        autoOpenBrowser: true,
        assetsSubDirectory: 'static',
        assetsPublicPath: '/',
        proxyTable: {
            '/api': {
                target: '服务器地址',
                changeOrigin: true,
                pathRewrite: {
                    '^/api': '/'
                }
            }
        },
        // CSS Sourcemaps off by default because relative paths are "buggy"
        // with this option, according to the CSS-Loader README
        // (https://github.com/webpack/css-loader#sourcemaps)
        // In our experience, they generally work as expected,
        // just be aware of this issue when enabling this option.
        cssSourceMap: false
    }

你这个是报的跨域错误,先确认下接口那边有没有问题!

跨域了,大兄弟。

  • 解决办法一:需要后端工程师配合设置 Access-Control-Allow-Origin 为 *

一定要设置 {emulateJSON: true},不然跨域不成功. 
如果Web服务器无法处理编码为application/json的请求,你可以启用emulateJSON选项。启用该选项后,请求会以application/x-www-form-urlencoded作为MIME type,就像普通的HTML表单一样
  • 解决办法二: this.$http.jsonp('...', { credentials: true }) 使用JSONP

题外: 建议以后使用axios vue-resource官方停止维护 使用起来 都差不多

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题