javascript - JavaScript Ajax 不能正常工作
问题描述
我正在用 JavaScript 编写一个自定义 HTTP 客户端,我的代码对我来说似乎没问题,但它会继续为我的请求获取 404。我已经在 NodeJS (ExpressJS) 服务器上运行它,它有这个处理程序。
app.post('/api/update', (req, res) => {
res.send(req.body);
});
这是我的js代码
const HTTPClient = (method, url, post) => {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open(method, url, true);
xhr.onload = () => {
const statusText = xhr.statusText || '';
// responseText is the old-school way of retrieving response (supported by IE9)
// response/responseType properties were introduced in XHR Level2 spec (supported by IE10)
const response = ('response' in xhr) ? xhr.response : xhr.responseText;
// normalize IE9 bug (http://bugs.jquery.com/ticket/1450)
let status = xhr.status === 1223 ? 204 : xhr.status;
// fix status code when it is 0 (0 status is undocumented).
// Occurs when accessing file resources or on Android 4.1 stock browser
// while retrieving files from application cache.
if (status === 0) {
status = response ? 200 : urlResolve(url).protocol === 'file' ? 404 : 0;
}
try {
return resolve(JSON.parse(response));
} catch (error) {
return resolve(response);
}
};
xhr.onerror = () => reject('Error');
xhr.ontimeout = () => reject('Timeout');
xhr.onabort = () => reject('Abort');
xhr.send(JSON.stringify(post) || null);
});
};
HTTPClient('post', '/api/update', defaultData).then(response => {
css.value = response.data;
console.log(response);
}, error => {
console.error(error);
});
注意:defaultData
是和对象
解决方案
您忘记添加Content-type
标题。
const HTTPClient = (method, url, post) => {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open(method, url, true);
// UPDATE
xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
xhr.onload = () => {
const statusText = xhr.statusText || '';
// responseText is the old-school way of retrieving response (supported by IE9)
// response/responseType properties were introduced in XHR Level2 spec (supported by IE10)
const response = ('response' in xhr) ? xhr.response : xhr.responseText;
// normalize IE9 bug (http://bugs.jquery.com/ticket/1450)
let status = xhr.status === 1223 ? 204 : xhr.status;
// fix status code when it is 0 (0 status is undocumented).
// Occurs when accessing file resources or on Android 4.1 stock browser
// while retrieving files from application cache.
if (status === 0) {
status = response ? 200 : urlResolve(url).protocol === 'file' ? 404 : 0;
}
try {
return resolve(JSON.parse(response));
} catch (error) {
return resolve(response);
}
};
xhr.onerror = () => reject('Error');
xhr.ontimeout = () => reject('Timeout');
xhr.onabort = () => reject('Abort');
xhr.send(JSON.stringify(post) || null);
});
};
HTTPClient('post', '/api/update', defaultData).then(response => {
css.value = response.data;
console.log(response);
}, error => {
console.error(error);
});
推荐阅读
- azure - PowerShell 脚本 - 是否可以检索 Azure 租户中包含的 PowerApps 的信息?
- java - 如何通过 for 循环为 CompletableFuture.allOf 添加期货?
- r - 将数据导入R后列名发生变化
- python - TypeError:无法腌制 _thread.RLock 对象 KerasClassifier
- javascript - 如何使用自定义按钮在本机反应中实现条形导航?
- css - 在 vue 中类删除和添加时反向动画
- validation - akka-http 在上传时验证文件扩展名
- javascript - 如何在 react-native 中从 Picker onValueChange 获取标签
- reactjs - 合并存储库子集的选项有哪些?
- aws-cdk - 如何使用 AWS CDK 将标签添加到 logretention 自定义函数