Skip to content

Commit

Permalink
修复json请求错误
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonxu committed Aug 29, 2017
1 parent 3853e15 commit 01fc8bb
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -4107,25 +4107,6 @@ var request = function (options, callback) {
options.type = options.method;
delete options.method;

// headers
if (options.headers) {
var headers = options.headers;
delete options.headers;
options.beforeSend = function (xhr) {
for (var key in headers) {
if (headers.hasOwnProperty(key) &&
key.toLowerCase() !== 'content-length' &&
key.toLowerCase() !== 'user-agent' &&
key.toLowerCase() !== 'origin' &&
key.toLowerCase() !== 'host') {
xhr.setRequestHeader(key, headers[key]);
}
}
};
} else {
options.headers = {};
}

// progress
if (options.onProgress) {
options.progress = options.onProgress;
Expand All @@ -4143,8 +4124,10 @@ var request = function (options, callback) {

// json
if (options.json) {
options.data = JSON.stringify(options.json);
options.data = options.body;
delete options.json;
delete options.body;
!options.headers && (options.headers = {});
options.headers['Content-Type'] = 'application/json';
}

Expand All @@ -4156,6 +4139,23 @@ var request = function (options, callback) {
}
}

// headers
if (options.headers) {
var headers = options.headers;
delete options.headers;
options.beforeSend = function (xhr) {
for (var key in headers) {
if (headers.hasOwnProperty(key) &&
key.toLowerCase() !== 'content-length' &&
key.toLowerCase() !== 'user-agent' &&
key.toLowerCase() !== 'origin' &&
key.toLowerCase() !== 'host') {
xhr.setRequestHeader(key, headers[key]);
}
}
};
}

var getResponse = function (xhr) {
var headers = {};
xhr.getAllResponseHeaders().trim().split('\n').forEach(function (item) {
Expand Down

0 comments on commit 01fc8bb

Please sign in to comment.