2016-07-28 160 views
0

是否有理由爲什麼我的Ajax調用不會在if語句內工作?我可能在這裏做了一些根本性的錯誤,因爲我對JS或Ajax沒有太多的經驗。由於JQuery Ajax調用不工作If語句

工作js函數是:

function filter(varType, varValue) { 
    var $products = $('#products'); 

    $.getJSON($SCRIPT_ROOT + '/filterSize/' + varValue) 

    .success(function (data) { 
     $products.empty(); // clear html from container 
     var elt0 = '<div class="page-content"><h1 id="shoes">Shoes</h1></div>'; 
     $products.append(elt0); 

     if (Object.keys(data.shoes).length === 0) { 
      none = '<span class="noresults">No results</span>'; 
      $products.append(none); 
     } 

     else { 
      var numItems = (Object.keys(data.shoes).length); 
      for (index = 0; index < numItems ; ++index) { 
       var elt1 = Flask.url_for("static", {"filename": data.shoes[index].img1}); 
       var elt2 = '<div id="item" class="col-md-4"><div class="products"><a href="shop/item/' + data.shoes[index].id + '"><h5>' + data.shoes[index].name + '</h5><img src="' + elt1 + '" alt="" width="200px" height="125px"></a></div>'; 
       $products.append(elt2); 
      } 
     } 
    }); 
} 

但如果我這樣做,它突然停止工作:

function filter(varType, varValue) { 
    var $products = $('#products'); 

    var x = 5 
    var y = 6 

    if (y > x) { 
     $.getJSON($SCRIPT_ROOT + '/filterSize/' + varValue) 
    } 

    .success(function (data) { 
     $products.empty(); // clear html from container 
     var elt0 = '<div class="page-content"><h1 id="shoes">Shoes</h1></div>'; 
     $products.append(elt0); 

     if (Object.keys(data.shoes).length === 0) { 
      none = '<span class="noresults">No results</span>'; 
      $products.append(none); 
     } 

     else { 
      var numItems = (Object.keys(data.shoes).length); 
      for (index = 0; index < numItems ; ++index) { 
       var elt1 = Flask.url_for("static", {"filename": data.shoes[index].img1}); 
       var elt2 = '<div id="item" class="col-md-4"><div class="products"><a href="shop/item/' + data.shoes[index].id + '"><h5>' + data.shoes[index].name + '</h5><img src="' + elt1 + '" alt="" width="200px" height="125px"></a></div>'; 
       $products.append(elt2); 
      } 
     } 
    }); 
} 
+0

因爲它是無效的javascript,您會在開發人員工具控制檯中看到的東西 - 您在if' – Suever

+0

停止工作之後的行中丟失了一個分號 - 您得到了哪些錯誤 –

回答

1
if (y > x) { 
      $.getJSON($SCRIPT_ROOT + '/filterSize/' + varValue) 

     .success(function (data) { 
      $products.empty(); // clear html from container 
      var elt0 = '<div class="page-content"><h1 id="shoes">Shoes</h1></div>'; 
      $products.append(elt0); 

      if (Object.keys(data.shoes).length === 0) { 
       none = '<span class="noresults">No results</span>'; 
       $products.append(none); 
      } 

      else { 
       var numItems = (Object.keys(data.shoes).length); 
       for (index = 0; index < numItems ; ++index) { 
        var elt1 = Flask.url_for("static", {"filename": data.shoes[index].img1}); 
        var elt2 = '<div id="item" class="col-md-4"><div class="products"><a href="shop/item/' + data.shoes[index].id + '"><h5>' + data.shoes[index].name + '</h5><img src="' + elt1 + '" alt="" width="200px" height="125px"></a></div>'; 
        $products.append(elt2); 
       } 
      } 
     }); 
} 

這是有效的語法。您需要移動括號內的.success。

1

你過早關閉了if-語句的花括號:

if (y > x) { 
    $.getJSON($SCRIPT_ROOT + '/filterSize/' + varValue) 
//} <-- TOO EARLY 
    .success(function (data) { 
     .... 
    }); 
} <-- SHOULD BE HERE INSTEAD 

在此之後結束if聲明回調。