我的JavaScript不能正常工作 [英] My javascript won't work as it should

查看:116
本文介绍了我的JavaScript不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在HTML中创建一个网站,该网站使用JavaScript接收JSON格式的数据,然后将这些数据注入我的网站。

问题是我找不到任何错误。我使用了示例代码。从我的网络服务器获取数据到我的HTML中。我使用上面链接的示例建立了与我的数据库的连接。但后来我想将现有代码移入此示例中在这里



在上面的例子中,我链接了我的JavaScript文件,它提取数据并创建一个div,其中包含 id ,同样的 id ,它是在JavaScript文件中分配的,用于获取数据。但它根本无法工作。在将被数据填充的 div 中,只是空的。

fetch.js

  $(document).ready(function (){
var output = $('#output');

$ .ajax({
url:'http://samcroft.co.uk/demos/ update-load-data-into-phonegap / landmarks.php',
dataType:'jsonp',
jsonp:'jsoncallback',
timeout:5000,
成功:函数(data,status){
$ .each(data,function(i,item){
var landmark ='< h1>'+ item.name +'< / h1>'
+'< p>'+ item.latitude +'< br>'
+ item.longitude +'< / p>';

output.append(landmark);
});
},
error:function(){
output.text('加载数据时发生错误。')
}
});
});

这是一个现场演示,代码在Web服务器上联机时加载。但不是当它在本地。是什么造成的?



http:/ / /codele.se/app/flip/debatt.html

解决方案

关于在您的网站上使用萤火虫,我发现您的html包含更新的数据,如下所示: b
$ b



问题是,您正在使用隐藏它的背景图片从您的网站中删除这行代码:

 < div style =z-index:3; left:512px; transform:rotateY(0deg); class =page cover> 

我得到这个输出:

< img src =https://i.stack.imgur.com/Ls2XF.pngalt =输出>



仔细观察我发现你在html中有两个具有相同ID输出的div,因此请确保只有一个div具有输出 id bcoz ID在html中应该是唯一的。




I'm trying to create a website in HTML which recieves JSON-formatted data using javascript and, then it will be injecting that data into my website.

The problem is I can't find any errors. I used this example code. To get data from my webserver into my HTML. I established a connection to my database using a example linked above. But then I wanted to move that existing code into this example over here.

In the example above, I've linked my javascript file which fetches the data and creates a div with the a id, the same id which is assigned in the javascript file that fetches the data. But it won't work at all. In the div that would be filled with data, is just empty.

fetch.js

$(document).ready(function(){
    var output = $('#output');

    $.ajax({
        url: 'http://samcroft.co.uk/demos/updated-load-data-into-phonegap/landmarks.php',
        dataType: 'jsonp',
        jsonp: 'jsoncallback',
        timeout: 5000,
        success: function(data, status){
            $.each(data, function(i,item){ 
                var landmark = '<h1>'+item.name+'</h1>'
                + '<p>'+item.latitude+'<br>'
                + item.longitude+'</p>';

                output.append(landmark);
            });
        },
        error: function(){
            output.text('There was an error loading the data.')
        }
    });
});

Here's a live demo, the code is loading when it's online on a webserver. But not when it's local. What is causing this?

http://codele.se/app/flip/debatt.html

解决方案

On using firebug on your site i found out that your html contains updated data as shown here:

The problem is you are using background image that hides it on removing this line of code from your site:

<div style="z-index: 3; left: 512px; transform: rotateY(0deg);" class="page cover"> 

I get this output:

on having a closer look I found that you have two div in html that have the same ID output, so make it sure that only one div has the output id bcoz ID should be unique in html.

这篇关于我的JavaScript不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆