无法将IP地址附加到“GET” URL

huangapple go评论83阅读模式
英文:

Javascript - Can't append ip address to "GET" url

问题

除了 IP 地址之外,所有内容都会追加到 URL 上。URL 的末尾应该看起来像这样,例如:ip=127.0.0.1。但 IP 地址未被添加。有什么想法?

这是代码...

英文:

Everything gets appended to the url except for the ip address. The end of the url should look like this for example; ip=127.0.0.1. But the ip address is not being added. Any ideas?

Here is the code...

<!-- User-Agent IP Code -->
<script type="application/javascript">
function getIP(json) {
	$.get( "https://hook.us1.make.com/s86ki3rt515ieg1gr3f9xxc5ufdu59zb?" +location.search.substring(1), "user=" + navigator.userAgent, "ip=" + json.ip);
}
</script>
<script type="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP"></script>
<!-- User-Agent IP Code -->

答案1

得分: 1

你可以使用URLSearchParams来构建查询字符串。

const params = new URLSearchParams(location.search);
params.append('user', navigator.userAgent);
params.append('ip', json.ip);
$.get("https://hook.us1.make.com/s86ki3rt515ieg1gr3f9xxc5ufdu59zb?" + params);
英文:

You can use URLSearchParams to construct the query string.

const params = new URLSearchParams(location.search);
params.append('user', navigator.userAgent);
params.append('ip', json.ip);
$.get("https://hook.us1.make.com/s86ki3rt515ieg1gr3f9xxc5ufdu59zb?" + params);

答案2

得分: 0

I believe the issue is in the "$.get" line. It looks like you're trying to concatenate the parameters in the string and they aren't properly formatted. In the $.get() function, it expects the params to be a single object. You can create an object with the params and put it in the function. Here is my code.

<!-- User-Agent IP Code -->
<script type="application/javascript">
function getIP(json) {
    let params = {
        user: navigator.userAgent,
        ip: json.ip
    };

    $.get("https://hook.us1.make.com/s86ki3rt515ieg1gr3f9xxc5ufdu59zb", params);
}
</script>
<script type="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP"></script>
<!-- User-Agent IP Code -->
英文:

I believe the issue is in the "$.get" line. I looks like your trying to concatenate the parameters in the string and they aren't properly formatted. In the $.get() function it expects the params to be a single object. You can create an object with the params and put it in the function. I'm not sure how the navigator.userAgent or json.ip string is from the code you have provided but if you pass the params it should give you a good idea. Here is my code.

<!-- User-Agent IP Code -->
<script type="application/javascript">
function getIP(json) {
    let params = {
        user: navigator.userAgent,
        ip: json.ip
    };

    $.get("https://hook.us1.make.com/s86ki3rt515ieg1gr3f9xxc5ufdu59zb", params);
}
</script>
<script type="application/javascript" src="https://api.ipify.org?format=jsonp&callback=getIP"></script>
<!-- User-Agent IP Code -->

答案3

得分: 0

It looks like there are a few syntax issues. I made some adjustments and wrapped your endpoint using the encodeURI method, which can prevent the URI-unfriendly characters from breaking the formattings.

$.get(encodeURI(
  "https://hook.us1.make.com/s86ki3rt515ieg1gr3f9xxc5ufdu59zb" +
    location.search.substring(1) +
    "?user=" +
    navigator.userAgent +
    "&ip=" +
    json.ip
));
英文:

It looks like there are a few syntax issues. I made some adjustments and wrapped your endpoint using the encodeURI method, which can prevent the URI-unfriendly characters from breaking the formattings.

     $.get(encodeURI(
      "https://hook.us1.make.com/s86ki3rt515ieg1gr3f9xxc5ufdu59zb" +
        location.search.substring(1) +
        "?user=" +
        navigator.userAgent +
        "&ip=" +
        json.ip
    ));

huangapple
  • 本文由 发表于 2023年5月17日 07:04:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76267601.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定