无法将IP地址追加到GET请求URL中
问题解决思路
你的核心问题是jQuery的$.get方法参数使用错误,导致IP参数无法正确追加到请求URL中。
错误原因
当前代码里,你把"user=" + navigator.userAgent和"ip=" + json.ip作为两个独立参数传给$.get,但$.get的第二个参数要求是一个键值对对象(或单个参数字符串),这种拆分传递的方式不符合方法规范,所以IP参数没有被识别。
修正方案
方案1:使用URLSearchParams规范处理参数(推荐)
这种方式能自动处理参数编码、拼接逻辑,避免手动拼接的潜在问题:
function getIP(json) { // 解析页面原有URL查询参数 const params = new URLSearchParams(location.search.substring(1)); // 添加user-agent和IP参数 params.append('user', navigator.userAgent); params.append('ip', json.ip); // 发起GET请求 $.get("https://hook.us1.make.com/s86ki3rt515ieg1gr3f9xxc5ufdu59zb", params); }
方案2:手动拼接完整URL字符串
如果偏好手动拼接,要确保所有参数都正确拼在URL的查询字符串中,同时注意编码:
function getIP(json) { const baseUrl = "https://hook.us1.make.com/s86ki3rt515ieg1gr3f9xxc5ufdu59zb"; const existingParams = location.search.substring(1); // 对特殊字符进行URL编码,避免参数传递异常 const userParam = `user=${encodeURIComponent(navigator.userAgent)}`; const ipParam = `ip=${encodeURIComponent(json.ip)}`; // 拼接完整URL,处理原有参数为空的情况 const fullUrl = existingParams ? `${baseUrl}?${existingParams}&${userParam}&${ipParam}` : `${baseUrl}?${userParam}&${ipParam}`; $.get(fullUrl); }
内容的提问来源于stack exchange,提问作者tdubb
相关产品推荐
相关产品推荐

