≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > vue.js

关于vue-resource报错450的解决方案

人气:452 时间:2019-04-07

这篇文章主要为大家详细介绍了关于vue-resource报错450的解决方案,具有一定的参考价值,可以用来参考一下。

感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!

本文介绍了关于vue-resource报错450的解决方案,分享给大家,具体如下:

 

一、基本使用:

 

 

1.页面引入

 

代码如下:


 import vueResource from 'vue-resource'
 Vue.use(vueResource)

 

2. 调取接口

 

代码如下:


Vue.http.post(url, {
 'data1': data1,
 'data2': 'data2'
}).then(response => {
 console.log('success', response)
}, response => {
 console.log('error', response)
})

 

二、报错450

 

定位错误信息:请求header没有完全一一对应。Content-Type: application/x-www-form-urlencoded; charset=UTF-8应为Content-Type: application/json; charset=UTF-8,检查页面代码,发现已经设置了

代码如下:


Vue.http.interceptors.push(function (request, next) {
 request.headers.set('Content-Type', 'application/json; charset=UTF-8')
 request.headers.set('Content-Type', 'application/json')
 next()
})

只是页面没有起作用而已,那究竟是什么原因导致页面设置的Content-Type失效了呢?继续追溯,发现跟这行代码有关

代码如下:


 // Vue.http.options.crossOrigin = true 
 // Vue.http.options.emulateHTTP = true
 Vue.http.options.emulateJSON = true //(跟这行代码有关)

 

三、分析

 

下面分别来讲一下这几行代码的用处,以及emulateJSON是怎么影响到Content-Type设置的。

1. Vue.http.options.crossOrigin

这个很明显是设置跨域的,此处不多讲。

2. Vue.http.options.emulateHTTP

参考地址:https://github.com/pagekit/vue-resource/blob/develop/src/http/interceptor/method.js

摘出源码

代码如下:


/**
 * HTTP method override Interceptor.
 */

export default function (request, next) {

  if (request.emulateHTTP && /^(PUT|PATCH|DELETE)$/i.test(request.method)) {
    request.headers.set('X-HTTP-Method-Override', request.method);
    request.method = 'POST';
  }

  next();
}

大概的意思就是如果请求方式为PUT|PATCH|DELETE,服务器又没法处理这几类请求的时候,设置Vue.http.options.emulateHTTP = true的话可以将X-HTTP-Method-Override设置为PUT|PATCH|DELETE,然后使用普通的post进行请求。

关于X-HTTP-Method-Override讲一下,它的使用场景是:

在某些HTTP代理不支持类似PUT|PATCH|DELETE这些类型HTTP请求的情况下,可以通过另一种完全违背协议的HTTP方法来"代理"。这种协议就是,使客户端发出HTTP POST请求并设置header里X-HTTP-Method-Override值为PUT|PATCH|DELETE。

3. Vue.http.options.emulateJSON

参考地址:https://github.com/pagekit/vue-resource/blob/develop/src/http/interceptor/form.js

摘出源码

代码如下:


/**
 * Form data Interceptor.
 */

import Url from '../../url/index';
import { isObject, isFormData } from '../../util';

export default function (request, next) {

  if (isFormData(request.body)) {

    request.headers.delete('Content-Type');

  } else if (isObject(request.body) && request.emulateJSON) {

    request.body = Url.params(request.body);
    request.headers.set('Content-Type', 'application/x-www-form-urlencoded');
  }

  next();
}

从第17行可以看到,如果设置了emulateJSON的话会默认加上这句

代码如下:


request.headers.set('Content-Type', 'application/x-www-form-urlencoded');

这就是为什么我们设置的Content-Type失效了。只要去掉Vue.http.options.emulateHTTP = true 或者直接置为false就可以了。

vue-resource(github)地址:https://github.com/pagekit/vue-resource

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持四海网。

本文来自:http://www.q1010.com/184/3227-0.html

注:关于关于vue-resource报错450的解决方案的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:vue.js

您可能感兴趣的文章

  • Axios学习笔记之使用方法教程
  • Vue AST源码解析第一篇
  • vue axios用法教程分析
  • Vue resource中的GET与POST请求的实例代码
  • Vue之Watcher源码解析(1)
  • 用Vue-cli搭建的项目中引入css报错的原因分析
  • 解决vue router使用 history 模式刷新后404问题
  • vue2.0中vue-cli实现全选、单选计算总价格的实例代码
  • vue-resource调用promise取数据方式分析
  • vue深入解析之render function code分析
上一篇:vue.js移动端app实战1:初始配置分析
下一篇:Vue resource中的GET与POST请求的实例代码
热门文章
  • Vue 报错TypeError: this.$set is not a function 的解决方法
  • vue实现动态添加数据滚动条自动滚动到底部的示例代码
  • vue项目设置scrollTop不起作用(总结)
  • vue项目中使用vue-i18n报错的解决方法
  • iview实现select tree树形下拉框的示例代码
  • 分析关于element级联选择器数据回显问题
  • vue项目打包后打开页面空白解决办法
  • 解决element ui select下拉框不回显数据问题的解决
  • element-ui table span-method(行合并)的实现代码
  • element-ui 设置菜单栏展开的方法
  • 最新文章
    • 理解vue ssr原理并自己搭建简单的ssr框架
    • vue favicon设置以及动态修改favicon的方法
    • vue-router启用history模式下的开发及非根目录部署方法
    • 从零开始在NPM上发布一个Vue组件的方法步骤
    • Element input树型下拉框的实现代码
    • Vue 报错TypeError: this.$set is not a function 的解决方法
    • Vue.js组件高级特性实例分析
    • 浅谈VueJS SSR 后端绘制内存泄漏的相关解决经验
    • 分析Vue.js自定义tipOnce指令用法实例
    • 浅谈vuex actions和mutation的异曲同工

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。