Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.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 如何将json数据加载到id=";“你好”;_Javascript_Jquery_Json - Fatal编程技术网

Javascript 如何将json数据加载到id=";“你好”;

Javascript 如何将json数据加载到id=";“你好”;,javascript,jquery,json,Javascript,Jquery,Json,HTML <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <script s

HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <title>doers.lk</title>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <style type="text/css" media="screen">

    </style>
    </head>
    <body>
      <p id="hello"></p>
    </body>
    </html>

我想将这个json数据加载到id=“hello”中。有什么更好的方法

$.getJSON('http://api.wipmania.com/jsonp?callback=?', function (data) {
  $("#hello").html('Latitude: ' + data.latitude +
        '\n Longitude: ' + data.longitude +
        '\n Country: ' + data.address.country);
});​
请看这篇文章:

jQuery提供了一种方法,可以让您获取/设置元素的内容。在回调中,您可以选择id上的元素,并使用
.html()
方法添加内容

$.getJSON('http://api.wipmania.com/jsonp?callback=?', function (data) {
  $("#hello").html('Latitude: ' + data.latitude +
        '<br /> Longitude: ' + data.longitude +
        '<br /> Country: ' + data.address.country);
});​
$.getJSON('http://api.wipmania.com/jsonp?callback=?,函数(数据){
$(“#你好”).html('Latitude:'+data.Latitude+
“
经度:”+data.Longitude+ “
国家:”+data.address.Country); });​

jquery代码

$.getJSON('http://smart-ip.net/geoip-json/?callback=?', function (data) {
    $("#hello").html('Latitude: ' + data.latitude +
        '<br /> Longitude: ' + data.longitude +
        '<br /> Country: ' + data.countryName);
});
$.getJSON('http://smart-ip.net/geoip-json/?callback=?,函数(数据){
$(“#你好”).html('Latitude:'+data.Latitude+
“
经度:”+data.Longitude+ “
国家:”+data.countryName); });
html


比wipmania.com更好地使用smart-ip.net

您最好使用本机jquery方法
$(…).html()
,更清楚的是不要混合使用本机js和jquery$(“#success”).load(“/id.html”,函数(response,status,xhr){if(status==“error”){var msg=“对不起,出现了一个错误:;$(“#error”).html(msg+xhr.status+”“+xhr.statusText);});var d=新日期();var x=document.getElementById(“演示”);x、 innerHTML=d.toLocaleDateString();我正在使用ajax加载id.html。(id.html)包含我上面提到的html和json代码,但现在它不适用于这两种代码methods@KayleeMackenzie这可能是一个新问题的主题,所以我建议你在描述新问题的地方问一个新问题。
$.getJSON('http://smart-ip.net/geoip-json/?callback=?', function (data) {
    $("#hello").html('Latitude: ' + data.latitude +
        '<br /> Longitude: ' + data.longitude +
        '<br /> Country: ' + data.countryName);
});