You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

POST请求提交FormData后重定向并自动填充表单数据问题

解决Fly Baghdad航班搜索表单提交后重定向并填充字段的问题

你遇到的核心问题是:fetch属于AJAX请求,不会触发浏览器的页面跳转行为,而官网的表单提交是浏览器原生表单提交流程,这才会触发页面导航并让目标页自动读取提交参数完成填充。以下是具体解决方法:

方法1:使用原生HTML表单提交(推荐,完全匹配官网逻辑)

直接模拟浏览器原生的表单提交,服务器会通过会话(Session)或Cookie暂存提交参数,跳转后预订页自动读取并填充字段。

// 创建隐藏的表单元素
const form = document.createElement('form');
form.action = 'https://booking.flybaghdad.net/ibe/IF/home?language=en';
form.method = 'POST';
form.style.display = 'none';

// 将参数转为表单隐藏输入项
Object.keys(datas).forEach(key => {
  const input = document.createElement('input');
  input.type = 'hidden';
  input.name = key;
  input.value = datas[key];
  form.appendChild(input);
});

// 添加到页面并触发提交
document.body.appendChild(form);
form.submit();

方法2:修改fetch请求,手动处理跳转

如果必须使用fetch,需要确保请求携带会话凭证,拿到响应后手动触发页面跳转,让服务器能通过会话关联你的提交参数:

await fetch("https://booking.flybaghdad.net/ibe/IF/home?language=en", {
  method: "POST",
  body: datas,
  headers: {
    "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
    "Content-Type": "application/x-www-form-urlencoded",
    // 模拟官网请求来源,部分服务器会校验该字段
    "Referer": "https://www.flybaghdad.net/en"
  },
  // 携带Cookie和会话凭证,确保服务器识别你的请求会话
  credentials: 'include',
  // 允许fetch跟随服务器重定向
  redirect: 'follow'
}).then(() => {
  // 手动跳转到预订页,此时会话中已保存提交参数
  window.location.href = "https://booking.flybaghdad.net/ibe/IF/home?language=en#searchForm";
});

方法3:核对请求参数与官网完全一致

用浏览器开发者工具(F12)查看官网提交表单时的Network请求详情,逐一对比你的请求:

  • 检查参数名是否完全匹配(比如官网可能用departureAirport而非你自定义的departure)
  • 确认是否有额外必填请求头(比如User-Agent、Origin)需要添加
  • 确保datas的编码格式符合application/x-www-form-urlencoded要求(特殊字符需正确编码)

内容的提问来源于stack exchange,提问作者coremraz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 18:33:27