访问网站时发送网站访客IP
Send website visitor IP when accessing website
我正在做一个个人项目,我想知道为什么不使用一种 ip 记录器来记录访问者的 ip 并将其发送到 Discord webhook。
该代码有效,但仅在本地有效。我尝试在 Glitch 上托管代码,但我在控制台中收到此错误:
(site) deleted for privacy/:1 Access to XMLHttpRequest at 'https://discord.com/api/webhooks/(deleted for privacy)' from origin '(site) deleted for privacy' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
discord.com/api/webhooks/(deleted for privacy) Failed to load resource: net::ERR_FAILED
grab.js:
$.getJSON("https://ipgeolocation.abstractapi.com/v1/?api_key=(my api key)", function(data) { //Make sure to use this api or use another but you got to change the values down below
var request = new XMLHttpRequest();
request.open("POST", "https://discord.com/api/webhooks/(the webhook)");
request.setRequestHeader('Content-type', 'application/json');
var t = {
username: "gameJet posta",
avatar_url: "https://www.origo.hu/i/1305/20130509-fogonosz-semion-mogilevich-orosz-maffiozo.jpg",
content: "",
embeds: [{
color: "4700374",
title: "szia lajos",
fields: [
{name: "Ip cim a latogatonak", value: data.ip_address},
{name: "varos", value: data.city},
{name: "orszag", value: data.country},
{name: "CONTINENT(nem tudom magyarul)", value: data.continent},
],
}]
};
request.send(JSON.stringify(t));
})
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <!--- Keep or else the script won't work --->
</head>
<body>
<script src="grab.js"></script>
<h1>Hello World</h1>
</body>
</html>
涵盖了您错误的大部分原因,简而言之,我想为您诊断错误并告诉您:
“No Access-Control-Allow-Origin header”问题通过 CORS Policy 解决,因为它认为你的脚本是恶意的(因为它显然是一个 IP 抓取器)你可以通过 CORS 解决这个问题您可以使用上面的链接答案设置的代理。
另外 Chromium does not allow CORS on localhost 这就是为什么你没有遇到那里的问题。
我正在做一个个人项目,我想知道为什么不使用一种 ip 记录器来记录访问者的 ip 并将其发送到 Discord webhook。 该代码有效,但仅在本地有效。我尝试在 Glitch 上托管代码,但我在控制台中收到此错误:
(site) deleted for privacy/:1 Access to XMLHttpRequest at 'https://discord.com/api/webhooks/(deleted for privacy)' from origin '(site) deleted for privacy' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
discord.com/api/webhooks/(deleted for privacy) Failed to load resource: net::ERR_FAILED
grab.js:
$.getJSON("https://ipgeolocation.abstractapi.com/v1/?api_key=(my api key)", function(data) { //Make sure to use this api or use another but you got to change the values down below
var request = new XMLHttpRequest();
request.open("POST", "https://discord.com/api/webhooks/(the webhook)");
request.setRequestHeader('Content-type', 'application/json');
var t = {
username: "gameJet posta",
avatar_url: "https://www.origo.hu/i/1305/20130509-fogonosz-semion-mogilevich-orosz-maffiozo.jpg",
content: "",
embeds: [{
color: "4700374",
title: "szia lajos",
fields: [
{name: "Ip cim a latogatonak", value: data.ip_address},
{name: "varos", value: data.city},
{name: "orszag", value: data.country},
{name: "CONTINENT(nem tudom magyarul)", value: data.continent},
],
}]
};
request.send(JSON.stringify(t));
})
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script> <!--- Keep or else the script won't work --->
</head>
<body>
<script src="grab.js"></script>
<h1>Hello World</h1>
</body>
</html>
“No Access-Control-Allow-Origin header”问题通过 CORS Policy 解决,因为它认为你的脚本是恶意的(因为它显然是一个 IP 抓取器)你可以通过 CORS 解决这个问题您可以使用上面的链接答案设置的代理。
另外 Chromium does not allow CORS on localhost 这就是为什么你没有遇到那里的问题。