We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
使用原生的XMLHttpRequest实现一个ajax
/** - - @param {*} options - url - data - method - async - timeout */ function ajax(options) { let url = options.url; const method = options.method.toLocaleLowerCase() || 'get'; const async = options.async != false; // default is true const data = options.data; const xhr = new XMLHttpRequest(); if (options.timeout && options.timeout > 0) { xhr.timeout = options.timeout; } return new Promise((resolve, reject) => { xhr.ontimeout = () => {reject && reject('请求超时');} xhr.onreadystatechange = () => { if (xhr.readyState === 4) { // 请求完成 if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) { resolve && resolve(xhr.responseText); } else { reject && reject(); } } } xhr.onerror = err => reject && reject(err); let paramList = []; let encodeData; if (data instanceof Object) { for (let key in data) { paramList.push(encodeURIComponent(key) + '=' + encodeURIComponent(data[key])); } encodeData = paramList.join('&'); } if (method === 'get') { const index = url.indexOf('?'); if (index === -1) { url += '?'; } else if (index !== url.length - 1) { url += '&'; } url += encodeData; } xhr.open(method, url, async); if (method === 'get') { xhr.send(null); } else { xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8'); xhr.send(encodeData); } }) }
The text was updated successfully, but these errors were encountered:
options.method.toLocalLowerCase()少了一个字母,toLocaleLowerCase()
Sorry, something went wrong.
thanks
No branches or pull requests
The text was updated successfully, but these errors were encountered: