SyntaxError:期望表达式,得到','

时间:2015-10-29 19:54:12

标签: jquery json

所以我环顾了stackoverflow,我发现之前可能已经问到这个问题,但是我无法绕过错误。希望你们能帮助我。我试图将变量添加到json api url,但是我收到以下错误:SyntaxError:expected expression,got','

我缺少什么?这是我的代码:

    <!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Temp</title>
  <style>
    html {margin:2em; font-size:2em; font-family:Helvetica, Arial, sans-serif;}
    h1 {margin:0 0 0.2em; color:#369;}
    img {float:left; margin-right:1em;}
    ul {float:left; margin:0; padding:0; list-style:none;}
  </style>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
  <section>
    <h1>Current Weather for </h1>
    <div class="img" id="data-img"></div>
    <ul>
      <li><b>Conditions:</b> <span id="data-current"></span></li>
      <li><b>Temperature:</b> <span id="data-temp"></span>&deg;</li>
      <li><b>Humidity:</b> <span id="data-humidity"></span>%</li>
      <li><b>Wind Speed:</b> <span id="data-wind"></span>mph</li>
      <li id="zip"></li>
      <li id="country"></li>
      <li id="api"></li>
    </ul>
  </section>
  <script>
        var zipcode = '27560';
        var countrycode = 'us'; 
        var appid = '743b2a88b6a3f61bdc87dee740b1a716';

        $.getJSON('http://api.openweathermap.org/data/2.5/weather?zip='+ zipcode +','+ countrycode +'&appid='+ appid +, function(data){ 
            console.log(data);
            $("#data-current").text(data.weather[0].description);
            $("#data-temp").text(data.main.temp);
            $("#data-humidity").text(data.main.humidity);
            $("#data-wind").text(data.wind.speed);
        })
        .fail(function(jqxhr, textStatus, error) {
        console.log("Request Failed" + textStatus + "," + error);
        }); 
        $('#zip').append(zipcode);
        $('#country').append(countrycode);
        $('#api').append(appid);
  </script>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

getJSON语句的第一行有一个额外的+符号。摆脱appid之后的+。

$.getJSON(... + appid +, function(data){...});