如何在spring mvc中给json中的URL

时间:2014-08-11 06:40:53

标签: javascript json spring

当我运行我的代码时,我无法访问getJSON函数中声明的url。

这是我的控制器代码

@RequestMapping(value = "branch")
@Controller
public class BranchController {
@Autowired(required = true)
VillageService villageService;


 @RequestMapping(value = "/addHome", method = RequestMethod.GET)
 public @ResponseBody 
 List getForm1(@RequestParam("districtId") int districtId, Model model, 
  HttpServletRequest request, HttpServletResponse response) {
    try {

        villageList= villageService.getDistrictVillageList(districtId);


    } catch (Exception er) {
        log.error("error in addLoanType=" + er);
    }

    return villageList;

  }

这是我的javascript代码

<script>
        $(document).ready(function() {
            $("select#district").change(function() {
              $.getJSON("/addHome", {districtId: $(this).val()}, function(j) {
                   var options = '';
                   for (var i = 0; i < j.length; i++) {
                   options += '<option value="' + j[i].id + '">' +                
                            j[i].name + '</option>';
                        }
                                  $("select#village").html(options);
                });
            });

        });

    </script>   

我的代码出了什么问题..请帮助我..

1 个答案:

答案 0 :(得分:0)

你有一个控制器映射,所以你的网址是

$.getJSON("branch/addHome.json",...);