Why is parsing a jSON response to a jQUERY ajax request not working
        Posted  
        
            by 
                Ankur
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Ankur
        
        
        
        Published on 2010-12-29T14:46:53Z
        Indexed on 
            2010/12/29
            14:54 UTC
        
        
        Read the original article
        Hit count: 306
        
I know there are several similar questions on SO, I have used some of them to get this far. I am trying to list a set of urls that match my input values. I have a servlet which takes some input e.g. "aus" in the example below and returns some output using out.print() e.g. the two urls I have shown below.
EXAMPLE
- Input: "aus"
 - Output: [{"url":"http://dbpedia.org/resource/Stifel_Nicolaus"},{"url":"http://sempedia.org/ontology/object/Australia"}]
 
Which is exactly what I want. I have seen that firebug doesn't seem to have anything in the response section despite having called out.print(jsonString); and it seems that out.print(jsonString); is working as expected which suggests that the variable 'jsonString' contains the expected values.
However I am not exactly sure what is wrong.
-------- The jQuery ---------
$(document).ready(function() {
    $("#input").keyup(function() {
        var input = $("#input").val();
        //$("#output").html(input);
        ajaxCall(input);
    });
});
function ajaxCall(input) {
//  alert(input);
    $.ajax({
        url: "InstantSearchServlet",
        data: "property="+input,
        beforeSend: function(x) {
              if(x && x.overrideMimeType) {
               x.overrideMimeType("application/j-son;charset=UTF-8");
              }
             },
        dataType: "json",  
        success: function(data) {
                 for (var i = 0, len = datalength; i < len; ++i) {
                     var urlData = data[i];
                     $("#output").html(urlData.url);
                     }                  
            }
          });
        }
------ The Servlet that calls the DAO class - and returns the results -------
public class InstantSearchServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
 System.out.println("You called?");
    response.setContentType("application/json");        
    PrintWriter out = response.getWriter();
    InstantSearch is = new InstantSearch();
    String input = (String)request.getParameter("property");
 System.out.println(input);
    try {
        ArrayList<String> urllist;
        urllist = is.getUrls(input);
        String jsonString = convertToJSON(urllist);
        out.print(jsonString);
  System.out.println(jsonString);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}
private String convertToJSON(ArrayList<String> urllist) {
    Iterator<String> itr = urllist.iterator();
    JSONArray jArray = new JSONArray();
    int i = 0;
    while (itr.hasNext()) {
           i++;
           JSONObject json = new JSONObject();
           String url = itr.next();
           try {
            json.put("url",url);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
           jArray.put(json);
        }
    String results = jArray.toString();
    return results;
}
} 
        © Stack Overflow or respective owner