jQuery JSONP (JSON with padding) – success method not called

February 23, 2011

Possible reasons for JSONP success method not getting called

  • In jQuery.getJSON method check if you are adding the
    callback=? param to your URL
  • Check if your server code serving JSONP request is returning proper JSONP response,

    For example sample server JSONP java code and response taken from IBM Developerworks article given below,
    Java code

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
    String jsonData = getDataAsJson(req.getParameter("symbol"));
    String output = req.getParameter("callback") + "(" + jsonData + ");";

    resp.setContentType("text/javascript");

    PrintWriter out = resp.getWriter();
    out.println(output);
    }

    JSONP Response Format

    jsonp1232617941775({"symbol" : "IBM", "price" : "91.42"});

To get a good understanding of JSONP you can read this IBM Developerworks article http://www.ibm.com/developerworks/library/wa-aj-jsonp1/