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

2020-06-12 21:22:53易采站长站整理

摘出源码


/**
* 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