0

I have to call and fetch data from rest API with in every second. So I call the method with time 1 sec. As follows.

var myVar = setInterval(function(){ getData1() }, 1000);

Following is my Javascript function which call the controller.

function getData1(){
    var url=CONTEXT_ROOT+"/login/getdashboarddata1";
    $.ajax({ 
        type: "POST",  
        url: url, 
        contentType: "application/json",
        dataType: "json",
        data:{},
            success: function(data){
                alert(data);                    

            },
         error: function(e){
             //alert(e);
         }
        });
}

This is my controller code

@RequestMapping(value="/getdashboarddata1", method=RequestMethod.POST)
    public JSONObject @ResponseBody getDashboardData1() throws JsonParseException, JsonMappingException, NullPointerException{ 

        RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:8080/r_f22bc0ac1fe48bce/dataService/lastdata/";
        String user = restTemplate.getForObject(url, String.class);

        System.out.println("user: "+user);
        JSONObject obj = null;
        try {
            obj = new JSONObject(user);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return obj;
    }

If I run the program then jsp does not shows any alert. but if I change the return type of in controller to String then it shows proper JSON string in ajax response.

i.e. [{"sno":"3618","data":"01","datetime":"2017-04-05 12:33:26.266"}]

If i carry this, then I am unable to get data from JSON string.

please tell me what is my issue. or is there any other way to do this.?

5
  • I didn't get what issue you are facing after changing return type of method as string? If you want to take value from reaponse, just use data.sno Commented Apr 8, 2017 at 6:48
  • if I return the JSON data as a string from controller to ajax response, then alert(data); it shows [{"sno":"3618","data":"01","datetime":"2017-04-05 12:33:26.266"}] Now if i am going to get particular data like alert(data.sno); then it shows undefine. Commented Apr 8, 2017 at 7:06
  • I have a doubt on contentType: "application/json", dataType: "json", with syntax issue. that is for double quotes or single quotes. Can you correct me If I wrong.. Commented Apr 8, 2017 at 7:20
  • you have json array, try with data[0].sno Commented Apr 8, 2017 at 7:34
  • Where is the users var defined? Commented Apr 8, 2017 at 7:43

2 Answers 2

1

You have json array

[{"sno":"3618","data":"01","datetime":"2017-04-05 12:33:26.266"}]

Then access it using index:

data[0].sno
Sign up to request clarification or add additional context in comments.

Comments

1

Simply return a String from getDashboardData1()

And then in AJAX success callback:

JSON.parse(data)[0]['sno'];

please tell me what is my issue. or is there any other way to do this.?

You can't access attributes of a string literal, it has to be a json object.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.