1

Here is my java code

public HashMap<Integer, Integer> getNoOfWidgetsFromUsername(final String username) //vikas- method to get pid from db.
{
HashMap<Integer, Integer> obMap = new HashMap<Integer, Integer>();
int numwidgets=getTotalWidgetsOfUser(PartnerID);
obMap.put(new Integer(1),PartnerID);
obMap.put(new Integer(2),numwidgets);
return obMap;
}

I put the above code in a function. I am calling that function from Javascript and returning values to Javascript.

Here is my Javascript code.

JSClientService.getNoOfWidgetsFromUsername(username, {
callback : function(data) {
//here i want to print hashmap values.
}
});

How can I access the data on the Javascript side?

6
  • 2
    How is the java object getting to the JS code? Commented Apr 26, 2012 at 4:46
  • I assume you're returning JSON, your question is vague Commented Apr 26, 2012 at 4:46
  • 1
    what technology are you using in your frontend? A java servlet? Commented Apr 26, 2012 at 4:46
  • what is JSCLientService ? Which library you are using for that. Please add tag. Commented Apr 26, 2012 at 4:47
  • Rakesh: JSclientService is used to call java script method, Commented Apr 26, 2012 at 4:48

1 Answer 1

2

It is a Java object, so you can not access it in javascript (i guess data will be undefined in your case).

Convert your java object to JSON object (something like this)

JSONObject json = new JSONObject();
json.put("res",obmap);

then in your js callback function ()

callback : function(data) {
  data = JSON.parse(data);
  for(var i in data.res){
      console.log(i); //key
      console.log(data.res[i]); //value  
  }
}

code not tested

Sign up to request clarification or add additional context in comments.

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.