2014-01-28 74 views
0

我有以下腳本。這個腳本是一個JSP具有其他(級聯選擇)後,填充一個國家,州和城市組合框spring ajax json - 動態下拉選擇

The script is suppose to fetch the data from the controller and populate list of states for the country selected. The function is called alert("inside ajax") is displayed and calls the controller method too. The controller fetches the list of states sucessfully and returns the list of states as "Set". when the control is passed back to the json alert("received data") doesnot pop up and nothing happens after that. There are no errors displayed too. 

I have "json-lib-2.4-jdk15.jar" in my library. Do I need any more settings? or what is that I'm missing? 

傑森Java腳本如下

<script type="text/javascript"> 
    $(document).ready(function() { 
     $('#countryName').change(
     function() { 
      alert("inside ajax"); 
      $.getJSON('${findStateURL}', { 
       countryName : $(this).val(), 
       ajax : 'true' 
      }, function(data) { 
       alert("received data"); 
       alert(data); 
       var html = '<option value="">State</option>'; 
       var len = data.length; 
       alert(len); 
       for (var i = 0; i < len; i++) { 
        html += '<option value="' + data[i].name + '">' 
          + data[i].name + '</option>'; 
        alert(html); 
       } 
       html += '</option>'; 
       $('#states').html(html); 
      }); 
     }); 
    }); 

控制器方法我裏面m呼叫是

@Controller 
    public class CountryController { 

     @Autowired 
     private CountryService countryService; 
     @RequestMapping(value = "/getStates.htm", method = RequestMethod.GET) 
     @ResponseBody public 
     Set<State> StatesForCountry(
     @RequestParam(value = "countryName", required = true) String countryName, Map<String, Object> map) { 
     System.out.println("countryName " + countryName); 
     System.out.println("Country Id "); 
     List<State> states = countryService.getAllStates(countryName); 
     Set<State> state_set = new HashSet<State>(states); 
     System.out.println(" no. of states set = " + state_set); 
     return state_set; 
     } 

控制器正在返回'set'中所需的值。但唯一的問題是我沒有收到任何數據在 警報(「收到的數據」);

回答

0

重新縮進代碼後,因爲我們不是機器:

$(document).ready(function() { 
$('#countryName').change(
    function() { 
     alert("inside ajax"); 
     $.getJSON(
      '${findStateURL}', 
      { 
       countryName : $(this).val(), 
       ajax : 'true' 
      }, 
      function(data) { 
       alert("received data"); 
       alert(data); 
       var html = '<option value="">State</option>'; 
       var len = data.length; 
       alert(len); 
       for (var i = 0; i < len; i++) { 
        html += '<option value="' + data[i].name + '">' 
        + data[i].name + '</option>'; 
        alert(html); 
       } 
       html += '</option>'; 
       $('#states').html(html); 
      } 
     ); 
    } 
); 
}); 

如果我讀的API Doc如果JSON文件包含語法錯誤,請求通常會失敗默默。

我建議你得到你的控制器返回的JSON(通過示例通過Firebug)並將你的JSON放入在線驗證器中。也許你會發現一個錯誤。

+0

我不使用螢火蟲,也不熟悉它。如果你能發現我在代碼中犯的錯誤,那對我會有幫助。 – sachin

+1

如果您開發Web應用程序,那麼如果您使用Firefox或在Chrome中使用開發工具,則應該安裝Firebug。您將在瀏覽器請求中看到錯誤。 你的代碼對我來說似乎很好,這就是爲什麼我認爲web應用程序返回的JSON可能存在錯誤。 – Mannekenpix