0

I am calling a below jquery call

var url = "demo/getResponseData"
$.get(url,function(responseData, status){
 console.log("dtFetched:",responseData);
 alert("status" + status);
});

The back end spring boot code is as below

@RequestMapping(path="demo/getResponseData")
    public JSONObject getResponseData()  throws IOException, JSONException{
        System.out.println("================================Called from JavaScript");
        HttpClientManager httpClientMgr = new HttpClientManager();
        List<JSONObject> responseString= httpClientMgr.getResponseData();
        JSONObject object = new JSONObject();
         object.put("data", responseString);
        System.out.println("============OUTPUT====================" + responseString);
        //return responseString;
        return object;
    }

I am able to see the output in java console, however 'responseData' is empty. What is the problem with the code?

Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85
GNT
  • 1
  • 3
  • Wrong variable name and missing `)` at the end. Please fix your typos – Phil Apr 18 '18 at 06:18
  • I just pasted a part of the code, please note that I do not have any script errors in the code. Is it something that each of the JSONObject in the List need to be mapped to a VO object? Currently I'm creating JSONObject and putting key and values and then adding that json to the list. – GNT Apr 18 '18 at 06:23
  • Have you tried looking in your browser's _Network_ console? What does the response body look like? – Phil Apr 18 '18 at 06:37
  • Hi Phil, thanks for the replying. I solved the issue by creation the VO objects and then putting it in the list. – GNT Apr 19 '18 at 10:20

2 Answers2

1

You are logging the wrong variable for response in javascript

var url = "demo/getResponseData"
$.get(url,function(responseData, status){
 console.log("dtFetched:",responseData); // <- fix this line
 alert("status" + status);
})
Anand Siddharth
  • 967
  • 1
  • 9
  • 30
  • Oh yeah, it is a typo, i'm using the correct variable. – GNT Apr 18 '18 at 06:17
  • hey. i am suing ur paytm package. i have done evrythng as u said. but wen i click the pay now button the page shows "transaction being processed" and then redirects me to a blank page like payment/api/status. why is it not taking me to paytm website? – Zubair Nazer Oliyat May 09 '18 at 18:08
0

Try adding the dataType

var url = "demo/getResponseData"
$.get(url,function(responseData, status){
    console.log("dtFetched:",responseData);
    alert("status" + status);
}, "json");

Also add @ResponseBody to the controller

public @ResponseBody JSONObject getResponseData() 

Also according to this answer in so, it is better to return the JSON as string.

@RequestMapping(path="demo/getResponseData")
public @ResponseBody String getResponseData()  throws IOException, JSONException{
    System.out.println("================================Called from JavaScript");
    HttpClientManager httpClientMgr = new HttpClientManager();
    List<JSONObject> responseString= httpClientMgr.getResponseData();
    JSONObject object = new JSONObject();
     object.put("data", responseString);
    System.out.println("============OUTPUT====================" + responseString);
    //return responseString;
    return object.toString();
}
StrikerVillain
  • 3,719
  • 2
  • 24
  • 41
  • As it is a RestController we do not need to add @ResponseBody spring boot will take care of it., however I solved the problem by creating the VO objects and then returning the list – GNT Apr 18 '18 at 08:07