Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将ClientIP添加到xhr post请求_Javascript_Ajax_Xmlhttprequest_Ip - Fatal编程技术网

Javascript 将ClientIP添加到xhr post请求

Javascript 将ClientIP添加到xhr post请求,javascript,ajax,xmlhttprequest,ip,Javascript,Ajax,Xmlhttprequest,Ip,我的ErrorHandler可以捕获错误,但我无法添加客户端ip。我不习惯ajax、xhr。。。我还在学习javascript。 我在后端获得以下输出: 错误{“timestamp”:“2020-10-12T22:14:57.113Z”,“ip”:{“readyState”:1},“消息”:“挂载钩子中的错误:\”引用错误:未定义asd} 为什么我要获得“ip”:{“readyState”:1}我如何获得我的真实ip window.onerror=函数(时间戳,ip, 消息、源、行号、列号、错误

我的ErrorHandler可以捕获错误,但我无法添加客户端ip。我不习惯ajax、xhr。。。我还在学习javascript。
我在后端获得以下输出:
错误{“timestamp”:“2020-10-12T22:14:57.113Z”,“ip”:{“readyState”:1},“消息”:“挂载钩子中的错误:\”引用错误:未定义asd}

为什么我要获得
“ip”:{“readyState”:1}
我如何获得我的真实ip

window.onerror=函数(时间戳,ip,
消息、源、行号、列号、错误){
常数托森={
“时间戳”:新日期(),
“ip”:$.get('https://ipinfo.io/ip', 
功能(数据IP){
返回{
数据IP
}    
}),
“信息”:信息,
“来源”:来源,
“lineo”:lineno,
“科尔诺”:科尔诺,
“错误”:错误,
};
const jsonString=JSON.stringify(toSend);
const xhr=new XMLHttpRequest();
xhr.open(“POST”,
"http://localhost:8090/api/auth/event");
setRequestHeader(“内容类型”,
“应用程序/json”);
send(jsonString);
返回false;
};

$。get
是一个异步函数,因此它不返回IP地址。相反,您需要将xhr调用包装在
$.get
函数中

window.onerror=函数(时间戳、ip、消息、源、行号、列号、错误){
$.get('https://ipinfo.io/ip,函数(dataIP){
常数toSend={
“时间戳”:新日期(),
“ip”:数据ip,
“信息”:信息,
“来源”:来源,
“lineo”:lineno,
“科尔诺”:科尔诺,
“错误”:错误,
};
const jsonString=JSON.stringify(toSend);
const xhr=new XMLHttpRequest();
xhr.open(“POST”http://localhost:8090/api/auth/event");
setRequestHeader(“内容类型”、“应用程序/json”);
send(jsonString);
});
返回false;
};
$.get
可能是返回jqXHR对象的,这就是为什么它会显示
readyState

另一件需要注意的事情是,由于
$.get
是jQuery,您可能还可以将其用于
xhr
调用。此外,根据提交
$.post
的位置,服务器可以添加ip地址,而不是在前端