My $.getJSON method is calling the servlet but callback function is not working.
this is my js file:
$(document).ready(function() {
$('p').addClass('highlight');
receiveData();
});
function receiveData(){
alert('receive data');
$.getJSON('ProcessForm', function(data){alert('hi')});
}
“Receive data” alert is coming on the screen, as well as call to ProcessForm Servlet is going, But the alert in the callback method is not coming.
This is what I had written in the ProcessForm Servlet
package com.nagarro.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
import com.nagarro.json.DictionaryList;
public class ProcesFormServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse response)
throws ServletException, IOException {
DictionaryList dictionaryList = new DictionaryList();
System.out.println("haan ji sir");
// Write response data as JSON.
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(new Gson().toJson(dictionaryList));
super.doGet(req, response);
}
protected void doPost(HttpServletRequest req, HttpServletResponse response)
throws ServletException, IOException {
DictionaryList dictionaryList = new DictionaryList();
System.out.println("haan ji sir");
// Write response data as JSON.
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(new Gson().toJson(dictionaryList));
super.doPost(req, response);
}
}
Anybody having any idea what could be the issue
Is it possible that you’re not returning well-formed JSON? That would cause jquery to not execute the callback code.
If you’re sure you are (run it through a JSON Lint site to double check), make sure you’re specifying Content-type: application/json in the header for the HTTP response