开发者

JSON in jsp file only accepts return values from my MVC controller that are null.

My JSP file:

<script type="text/javascript">
    function suggest() {

     $.getJSON("/practice-webapp/getOrgList.htm", 
        { searchee: $("#searchee").val() }, 
          function(data) { alert(data); });

    }

    </script>

Search Org Name: <input id="searchee" onkeyup="suggest()" />

My Controller:

@RequestMapping(value = "/getOrgList.htm",开发者_Go百科 method = RequestMethod.GET)
    public @ResponseBody String getOrgList(@RequestParam String searchee) {

        System.out.println("RECEIVED: " + searchee );
        return searchee;
    }

Whenever I enter a string in the search field, the "alert(data)" is not executed. However, if I use backspace until the field is empty, the "alert(data)" is executed, but obviously it contains a null value. I am just testing the json capability. All I want is to display the string returned by my controller. (By the way, the searchee value is indeed received by the controller.)


You are returning a string from getOrgList action where as getJSON expects a well formed json string because of which you are not seeing anything in the response. Try to send the below

 public @ResponseBody String getOrgList(@RequestParam String searchee) {

        System.out.println("RECEIVED: " + searchee );
        return "{ 'searchee': '"+searchee+"' }";
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜