【问题标题】:how to give the URL in the json in spring mvc如何在spring mvc中给出json中的URL
【发布时间】:2014-08-11 06:40:52
【问题描述】:

当我运行我的代码时,我无法访问在 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>   

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

【问题讨论】:

  • 您有错误 404 还是其他错误?你是如何部署你的应用程序的,什么容器?你用maven吗?你的类路径中有杰克逊吗?

标签: javascript json spring


【解决方案1】:

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

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

【讨论】:

  • 你得到了什么回应?
  • 在浏览器中点击 /branch/addHome.json 的 url 并查看状态码,它是否点击了控制器中的方法?
  • 现在我已经能够访问控制器中的 url,但是当我将返回的列表放入警报时没有显示...
  • $.getJSON("addDistrictVillage", {districtId: $(this).val()},{ ajax: 'true' } ,function(j) { alert(j);
猜你喜欢
  • 2017-10-03
  • 2016-12-30
  • 1970-01-01
  • 2014-05-26
  • 2016-09-06
  • 2016-12-28
  • 2012-06-06
  • 2015-08-11
  • 2011-03-06
相关资源
最近更新 更多