Json not able to access via dot operator

180 Views Asked by At

I am using a4j jsFunction to send data to server and receive Json from server

<a4j:jsFunction name="submitData"
action="#{imageRetriveBean.saveData}" data="#{responseNodesPathsBean}"
oncomplete="processData(event.data)">
    <a4j:param name="param1" noEscape="true" value="myFunction()"
    assignTo="#{imageRetriveBean.requestJsonMsg}" />
    <a4j:param name="param2" noEscape="true" value="getFloorNo()"
    assignTo="#{imageRetriveBean.floorNo}" />
</a4j:jsFunction>

In the processData function below

function processData(data)
{
    console.log(data);
    var dataObj = data.responseJsonMsg;
}

The console.log prints the data correctly. Following is the output.

({responseJsonMsg:"{//my data}"})

but I am not able to access the data using the data.responseJsonMsg

The console gives the error

TypeError: data is undefined

Images of code where error occurs and the error output on Chrome

https://docs.google.com/file/d/0B2LrAStQ7RleOUtHMTRfT2sxams/edit?usp=sharing

https://docs.google.com/file/d/0B2LrAStQ7RleU09Vdll1VURaT2c/edit?usp=sharing

Please help me know where I could be wrong. Thanks

1

There are 1 best solutions below

0
On

if that is the output of console.log, then it is not a javascript object, you need to parse it JSON.parse(data), though from what you are showing that is not a valid json string anyway so it would still error out. It would need the parenthesis removed, responseJsonMsg would need quoted etc. You need to have your server side code output a valid json string

it should look like, if //my data is supposed to be actual data

{"responseJsonMsg":{ "name":"patrick", "where":"here" }}