0

I have looked at this example but I am still unable to retrieve the JSON Object in the jsp. Here's the code in my MyCalendarController.java class:

public class MyCalendarController implements Controller{

    public ModelAndView handleRequest(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        if("Add".equals(request.getParameter("action"))){
...
            JSONObject jObj = new JSONObject();
            jObj.put("test", "Success");
            response.getWriter().write(jObj.toString());
...
         }
     return new ModelAndView("mycalendar", "model", myModel);
}

and here's how I'm trying to retrieve it in jsp but the alert always says 'undefined'

var queryString = "?action=Add";
    queryString +=  "&t=" + title;
    queryString +=  "&sDT=" + stDate + "T" + stHour + ":" + stMin + ":00";
    queryString +=  "&eDT=" + eDate + "T" + eHour + ":" + eMin + ":00";
$.ajax({
        type:"GET",
        url: "mycalendar.htm" + queryString,
        success: function(response){
                    alert(response.test);
        }
});

Note: I am trying to create the JSON Object when the ajax call is made to the class from the jsp. I am new to ajax and javascript so must be doing something wrong... Please help!!!

In the above mentioned code, the response.responseText property is 'undefined'. But I tried it another way:

var ajaxRequest;

try{                                       
    ajaxRequest = new XMLHttpRequest();
}catch (e){
    try{
        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    }catch (e) {
        try{
            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }catch (e){
            alert("Your browser broke!");
            return false;
        }
    }
 }

 ajaxRequest.onreadystatechange = function(){
     if(ajaxRequest.readyState == 4){
        alert(ajaxRequest.responseText);
        alert("test: " + ajaxRequest.test);
     }
}



var queryString = "?action=Add";
queryString +=  "&t=" + title;
queryString +=  "&sDT=" + stDate + "T" + stHour + ":" + stMin + ":00";
queryString +=  "&eDT=" + eDate + "T" + eHour + ":" + eMin + ":00";

ajaxRequest.open("GET", "mycalendar.htm" + queryString, true);
ajaxRequest.send(null);

This way I get the ajaxRequest.responseText but the alert("test: " + ajaxRequest.test); still shows undefined

Community
  • 1
  • 1

2 Answers2

1
var a =  '<%=DropDown.returnList()%>';
var countryObj = JSON.parse(a);
var s = $('#selectCountry');  
 for(var val in countryObj) 
      {          
       $('<option />', {value: val, text: countryObj[val]}).appendTo(s); 
      } 
B Ravi
  • 9
  • 1
0

Try to alert(response.responseText),I am not sure.

刘伟科
  • 432
  • 3
  • 12
  • alert(response.responseText) is undefined too...when accessing the object using the first method. In the second method, the ajaxRequest.responseText returns the response. However, the ajaxRequest.test is still undefined. Is this the right way to access the object? alert("test: " + ajaxRequest.test); Anyone, any insight... please!!! – user1002302 Oct 21 '11 at 02:09