0

i want to return a JSON from primefaces to d3 with the help of d3.json,but it is throwing error.

My primefaces bean,

@ManagedBean
@SessionScoped
public class JsonBean implements Serializable {

private static final long serialVersionUID = 1L;

JsonExample jse=new JsonExample();


public void prepareChartDate() {

    RequestContext reqCtx = RequestContext.getCurrentInstance();
    reqCtx.addCallbackParam("chartData",jse.createJson());
}
}

jse.createJson() will give the json as

{"A":10,"B":20,"C":30,"weekdays":["Sun","Mon","Tues"]}

and the d3js code which i m calling in .xhtml file is

<script src="http://d3js.org/d3.v3.js"></script>

<script>
var data;
d3.json("#{jsonBean.prepareChartDate()}",function(error, json) {
  if (error) return console.warn(error);
  data = json;
  console.log(data);
  //console.log("hello");

});

</script>

when i m printing in the console it is giving error in firebug. firbug screenshot

maali
  • 134
  • 3
  • 14
  • Like the error states, there is a syntax error in your JSON. {"A":10,"B":20,"C":30 should be {"A":"10","B":"20","C":"30" – Emil Kaminski Jul 29 '13 at 07:43
  • @EmilKaminski : I changed the JSON tothis,`{"A":"10","B":"20","C":"30","weekdays":["Sun","Mon","Tues"]}`,but then also i m getting this exception,`missing ) after argument list d3.json("{"A":"10","B":"20","C":"30","weekdays":["Sun","Mon","Tues"]}",function(... ` in firebug,is there anything wrong with d3.json function or how i need to call the ajax request from d3 to jsf. – maali Jul 29 '13 at 09:17
  • I am no expert in d3 but from what i can see in the documentation, is that the first parameter of the d3.json method is the url to the json file, and not the json output itself, like: d3.json("path/to/file.json", function(error, json) {... – Emil Kaminski Jul 29 '13 at 10:29
  • @EmilKaminski Thanks,i am actually having a problem with JSF syntax in xhtml,i am able to invoke the servlet from d3.json function but,`#{jsonBean.prepareChartDate()}` This is giving me error.Can we mix a servlet in JSF?if so,can u provide any link or suggestion. – maali Jul 29 '13 at 11:05
  • 1
    Here is a link where someone is pasting json data into a highchart, perhaps it can be of help: http://stackoverflow.com/questions/16414903/how-to-send-json-using-jsf2-primefaces-request-context-to-highchart – Emil Kaminski Jul 29 '13 at 12:00
  • @EmilKaminski Thanks i Shall i have look onto that. – maali Jul 29 '13 at 14:03

1 Answers1

0

You don't call a method in this way: #{jsonBean.prepareChartDate()}. You have to create a class like this:

@ManagedBean
@SessionScoped
public class JsonBean implements Serializable {
  private static final long serialVersionUID = 1L;
  public String getChartDate() {
    String jsonData;
    /* jsonData valorization */
    return jsonData;
  }
}

and call the method with #{jsonBean.chartDate}

Giampaolo
  • 133
  • 2
  • 8
  • You don't call a GETTER this way. You can call a method this,way, but it is not the right aay in this context. Please improve your answer – Kukeltje Dec 28 '15 at 10:12