1

I am trying to pass Json as callback parameter. Can anyone correct the javascript to get the values from the json object?

javascript

function renderTopic(xhr,status,args){
        alert("iam In renderTopic");
        var topicObject = $.parseJSON(args.topicJSON);
        for(var x in topicObject){
            alert(x.topicBody);
        };
    }

Server side

   jGenerator.writeFieldName("topicBody");
            jGenerator.writeStartArray();
                 ...............
            jGenerator.close();
             context = RequestContext.getCurrentInstance();
             System.out.println("JSON output in string "+out.toString())

xhtml

<p:commandLink action="#{topicController.listAllTopics}"
                id="topicListAllCmdLink" value="" oncomplete="javascript:renderTopic(xhr, status,args)"/>

http response

<partial-response><changes><update id="javax.faces.ViewState"><![CDATA[4091946826703479326:-2361306414195161728]]></update><extension ln="primefaces" type="args">{"topicJSON":"{\"topicBody\":[\"Test10\",\"Test22\",\"Test4\",\"Test11\"]}"}</extension></changes></partial-response>
user1595858
  • 3,700
  • 15
  • 66
  • 109

1 Answers1

0

Every thing seems to be ok. Do you see any error? or any output that you can tell us about.

See this link to get a better description: Best method for passing Data from Java/JSF2 bean to Javascript/jQuery Components as return values

Community
  • 1
  • 1
Mehdi
  • 4,396
  • 4
  • 29
  • 30
  • I believe it is failing at parsing the jquery since it has backslash before double quote. – user1595858 Aug 30 '12 at 11:19
  • How to avoid primefaces from prepending "\" in the json output? – user1595858 Aug 30 '12 at 11:46
  • are you sure that primefaces add those backslashes? could you please paste the code where you generate and append the json to callback, i see the current code is not complete. – Mehdi Aug 30 '12 at 12:29