如何在Chrome扩展中用JavaScript实现类似Python的POST请求?
解决Chrome扩展中POST请求失败的问题
你的请求头里同时指定了multipart/form-data和application/json两种Content-Type,这是无效格式,服务器无法正确解析请求体,这就是JS请求失败的核心原因。先确认你Python代码里实际用的是哪种提交格式,再对应下面的JS实现:
情况1:用multipart/form-data提交(对应请求头里的boundary)
如果Python里是通过表单格式提交的,JS里要构造FormData对象,不需要手动设置Content-Type(浏览器会自动生成带正确boundary的请求头):
Fetch API实现
const miData = { 'form_key': 'xxxxxxxxx', 'user': 'yyyyyyyy', 'action': '1' }; const formData = new FormData(); for (const key in miData) { if (miData.hasOwnProperty(key)) { formData.append(key, miData[key]); } } fetch('https://www.midomain.com/es/info/', { method: 'POST', headers: { 'Accept': '*/*', 'Cache-Control': 'no-cache', 'Pragma': 'no-cache', 'X-Requested-With': 'XMLHttpRequest', // 不要手动设置Content-Type,浏览器会自动处理 }, body: formData, credentials: 'same-origin' // 保持同域Cookie,和Python请求一致 }) .then(response => response.text()) // 若返回JSON则用response.json() .then(data => console.log(data)) .catch(error => console.error('请求失败:', error));
XMLHttpRequest实现
const miData = { 'form_key': 'xxxxxxxxx', 'user': 'yyyyyyyy', 'action': '1' }; const formData = new FormData(); for (const key in miData) { if (miData.hasOwnProperty(key)) { formData.append(key, miData[key]); } } const xhr = new XMLHttpRequest(); xhr.open('POST', 'https://www.midomain.com/es/info/', true); xhr.setRequestHeader('Accept', '*/*'); xhr.setRequestHeader('Cache-Control', 'no-cache'); xhr.setRequestHeader('Pragma', 'no-cache'); xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); // 不要设置Content-Type,XHR会自动处理 xhr.onload = function() { if (xhr.status >= 200 && xhr.status < 300) { console.log(xhr.responseText); } else { console.error('请求错误:', xhr.statusText); } }; xhr.onerror = function() { console.error('网络请求失败'); }; xhr.send(formData);
情况2:用application/json提交
如果Python里是发送JSON格式的数据,那JS里要把对象转成JSON字符串,并正确设置Content-Type:
Fetch API实现
const miData = { 'form_key': 'xxxxxxxxx', 'user': 'yyyyyyyy', 'action': '1' }; fetch('https://www.midomain.com/es/info/', { method: 'POST', headers: { 'Accept': '*/*', 'Cache-Control': 'no-cache', 'Pragma': 'no-cache', 'X-Requested-With': 'XMLHttpRequest', 'Content-Type': 'application/json' }, body: JSON.stringify(miData), credentials: 'same-origin' }) .then(response => response.text()) .then(data => console.log(data)) .catch(error => console.error('请求失败:', error));
XMLHttpRequest实现
const miData = { 'form_key': 'xxxxxxxxx', 'user': 'yyyyyyyy', 'action': '1' }; const xhr = new XMLHttpRequest(); xhr.open('POST', 'https://www.midomain.com/es/info/', true); xhr.setRequestHeader('Accept', '*/*'); xhr.setRequestHeader('Cache-Control', 'no-cache'); xhr.setRequestHeader('Pragma', 'no-cache'); xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.onload = function() { if (xhr.status >= 200 && xhr.status < 300) { console.log(xhr.responseText); } else { console.error('请求错误:', xhr.statusText); } }; xhr.onerror = function() { console.error('网络请求失败'); }; xhr.send(JSON.stringify(miData));
额外注意事项
- Chrome扩展权限:在
manifest.json里必须添加目标域名的权限,比如:
{ "permissions": [ "https://www.midomain.com/*" ] }
- 请求头匹配:尽量和Python请求的头保持一致,比如User-Agent,若服务器校验该字段,可在headers里添加对应的字符串。
- 调试建议:用Chrome开发者工具的Network面板对比Python和JS请求的差异,重点检查Content-Type、请求体格式是否完全一致。
内容的提问来源于stack exchange,提问作者Pizzaboy
相关产品推荐
相关产品推荐

