在HTML链接单击上调用Javascript函数时出错
我有一个文本框,一个HTML链接和一个JavaScript函数 JavaScript函数用于获取客户端计算机的IP地址。当我点击链接时,我试图调用这个函数&在我的文本框中显示结果。但是,我没有得到结果 这就是我得到的错误在HTML链接单击上调用Javascript函数时出错,javascript,jquery,html,textbox,ip-address,Javascript,Jquery,Html,Textbox,Ip Address,我有一个文本框,一个HTML链接和一个JavaScript函数 JavaScript函数用于获取客户端计算机的IP地址。当我点击链接时,我试图调用这个函数&在我的文本框中显示结果。但是,我没有得到结果 这就是我得到的错误 Uncaught TypeError: Cannot read property 'ip' of undefined 我的文本框: <input type="text" id="txtmyip" name="txtmyip"readonly> 函数Displa
Uncaught TypeError: Cannot read property 'ip' of undefined
我的文本框:
<input type="text" id="txtmyip" name="txtmyip"readonly>
函数
DisplayIP
需要一个参数response
,但当使用onclick=“DisplayIP()”
调用该函数时,不会为该参数传入任何值。因此,它现在尝试读取未定义的响应的response.ip
函数DisplayIP
需要一个参数response
,但当使用onclick=“DisplayIP()”
调用该函数时,不会为该参数传入任何值。因此,它现在正在尝试读取response.ip
,对于未定义的响应
,,您需要分离JSONP请求函数和显示函数。例如:
function displayIP() {
var script = document.createElement("script");
script.src = "http://www.telize.com/jsonip?callback=populateIP";
document.head.appendChild(script);
}
function populateIP(response) {
document.getElementById("txtmyip").value = response.ip;
}
演示:您需要分离JSONP请求函数和显示函数。例如:
function displayIP() {
var script = document.createElement("script");
script.src = "http://www.telize.com/jsonip?callback=populateIP";
document.head.appendChild(script);
}
function populateIP(response) {
document.getElementById("txtmyip").value = response.ip;
}
演示:实际上响应
也是未定义的
实际上响应
也是未定义的
function displayIP() {
var script = document.createElement("script");
script.src = "http://www.telize.com/jsonip?callback=populateIP";
document.head.appendChild(script);
}
function populateIP(response) {
document.getElementById("txtmyip").value = response.ip;
}