【问题标题】:JavaScript unable to display weatherJavaScript 无法显示天气
【发布时间】:2016-10-03 03:20:49
【问题描述】:

我正在使用 JSFiddle example - 我已将 CSS、JavaScript 和 HTML 放在一个文件中,并正在使用 WampServer 运行它。没有显示天气信息。我仔细检查了所有内容,但找不到问题。有什么建议吗?

代码:

<!DOCTYPE html>
<html>
<head>

<style>
.weatherModule {
    background:#f2f2f2;
    height:150px;
    width:250px;
    border: 1px dashed #ccc;
    padding: 1em;
}
.currentConditions {
    float: left;
}
.weatherModule {
    padding: 1em;
}

.currentIcon {
    float: left;
    margin: 0 .75em;
}

</style>

<script type="text/javascript">

displayWeather(){

$.ajax
    url: 'http://api.wunderground.com/api/36b799dc821d5836/conditions/q/PA/Horsham.json'
    dataType: 'jsonp'
    data: 'url'
    success: (data) ->
        for index, result of data
                temp = Math.round result.temp_f
                icon = result.icon_url
                weather = result.weather
                $('p.currentConditions').html "Currently  #{temp} &deg; F and #{weather}"
                $('div.currentIcon').html "<img src='#{icon}' >"

}
</script>
</head>
<body>
<div class="weatherModule">
<p class="currentConditions"></p>
<div class="currentIcon"></div>
</div>
<form>
<button type="button" onclick="displayWeather()">Display Weather</button>
</form>
</body>
</html>

【问题讨论】:

  • 你的小提琴适合我。
  • $.ajax({ [parameters] });
  • Javascript 控制台中是否有任何错误?
  • @u_mulder 这是coffeescript,你省略了很多括号。
  • @Sumurai8 这就是他所说的:它在 jsfiddle 中工作,而不是在他的真实网站上。

标签: javascript coffeescript


【解决方案1】:

尝试添加

crossDomain: true,

到您的 $.ajax 选项。

【讨论】:

  • 只有在向 same origin 发出请求时才有效,该请求会被 redirect 到不同的来源。没有任何迹象表明这里正在发生这种情况。
猜你喜欢
  • 1970-01-01
  • 2015-02-08
  • 2018-08-31
  • 1970-01-01
  • 2011-08-05
  • 2014-04-15
  • 1970-01-01
  • 2020-09-09
  • 1970-01-01
相关资源
最近更新 更多