2016-12-02 4 views
0

.find()が成功し、データが存在する場合、マイコードから評価が返されます。 itemnameのデータが見つからない場合はエラーを返したい。ファイルnameOfBusiness = Cafeの場合、レーティング3が返されますが、nameOfBusiness = Restaurantの場合は「Rating not published yet」というエラーが返されます。Ajax - 変数が何も返さなかった場合にエラーを返したい

データは私のxmlを形成:

$.ajax({ 
    type: 'GET', 
    url: 'someUrl.xml', 
    dataType: 'xml', 
    success: function(xml){ 
     var rating = "";         
     $(xml).find('resultitem itemname:contains("'+nameOfBusiness+'")').each(function(){ 
     rating = $(this).parent().find('rating').text() 
       if(rating==""){ 
        alert("Rating is not published yet."); 
       } else { 
        alert(rating); 
       } 

      }); 
     } 
    }); 

答えて

0

このような何か:

<resultitem> 
    <itemname>Cafe</itemname> 
    <rating>3</rating> 
</resultitem> 

私は、Ajaxコード持って

$.ajax({ 
    type: 'GET', 
    url: 'someUrl.xml', 
    dataType: 'xml', 
    success: function(xml){ 
     var rating = "";         
     var found = $(xml).find('resultitem itemname:contains("'+nameOfBusiness+'")'); 

     if (!found.length) { //HERE 
      alert('error'); 
      return; 
     } 

     found.each(function(){ 
      rating = $(this).parent().find('rating').text(); 
      if(rating==""){ 
       alert("Rating is not published yet."); 
      } else { 
       alert(rating); 
      } 

     }); 
    } 
}); 
+0

それが動作OMGを!!!どうもありがとうございます! –

関連する問題