在测试期间,如果带有JSON数据的HTTP响应包含'&',如果我删除'&',则primefaces会失败有用。我发送的数据是一个URL,我需要以编码格式发送网址吗?如果任何数据(不是网址)包含'amp'或任何类型的此类字符,是否会发生此问题?在这种情况下,最好的方法是什么。
<h:form prependId="false" id="getAllTopics" style="display:none;">
<p:commandLink action="#{topicController.listAllTopics}" id="topicListAllCmdLink" value=""
oncomplete="javascript:renderTopic(xhr, status,args)" />
</h:form>
function renderTopic(xhr,status,args){
jsonArray=[];
console.log(args.topicJSON);
jsonArray = $.parseJSON(args.topicJSON);
.....
}
从Google开发人员工具包中退出
<partial-response><changes><update id="javax.faces.ViewState"><![CDATA[4297043168621098325:5437886159631978839]]></update><extension ln="primefaces" type="args">{"topicJSON":"[{\"topicBody\":\"http://www.youtube.com/watch?v=m3ZyU98N3Fk&feature=relmfu \",\"videoAudioUrl\":\"\",\"topicGuid\":5600,\"userGuid\":0,\"imageURL\":\"\",\"topicOwnUserName\":\"srikanth marni\",\"topicCommentList\":[],\"topicUpdateTime\":1346817736000}]"}</extension></changes></partial-response>
Uncaught TypeError: Cannot read property 'topicJSON' of undefined circle_topic.js.jsf:150
renderTopic circle_topic.js.jsf:150
PrimeFaces.ab.oncomplete circle.jsf:175
k.complete primefaces.js.jsf:1
b.Callbacks.bv jquery.js.jsf:16
b.Callbacks.bE.fireWith jquery.js.jsf:16
bF jquery.js.jsf:23
b.ajaxTransport.send.bv
答案 0 :(得分:0)
目前(v.5.2),primefaces部分响应编写器没有为XML转义JSON,这会导致字符出现问题&#39;&#39;&#39;&gt;&#39;&#39;,&#39; ;&安培;&#39;等等。
为了使这个工作,我修补了方法encodeCallbackParams 在org.primefaces.context.PrimePartialResponseWriter中,如下所示,而XML是org.json.XML:
private void encodeCallbackParams(Map<String, Object> params) throws IOException, JSONException {
if (params != null && !params.isEmpty()) {
startExtension(CALLBACK_EXTENSION_PARAMS);
write("{");
for (Iterator<String> it = params.keySet().iterator(); it.hasNext();) {
String paramName = it.next();
Object paramValue = params.get(paramName);
if (paramValue instanceof JSONObject) {
String json = ((JSONObject) paramValue).toString();
json = XML.escape(json);
write("\"");
write(paramName);
write("\":{");
write(json.substring(1, json.length() - 1));
write("}");
} else if (paramValue instanceof JSONArray) {
String json = ((JSONArray) paramValue).toString();
json = XML.escape(json);
write("\"");
write(paramName);
write("\":[");
write(json.substring(1, json.length() - 1));
write("]");
} else if (isBean(paramValue)) {
String json = new JSONObject(paramValue).toString();
json = XML.escape(json);
write("\"");
write(paramName);
write("\":");
write(json);
} else {
String json = new JSONObject().put(paramName, paramValue).toString();
json = XML.escape(json);
write(json.substring(1, json.length() - 1));
}
if (it.hasNext()) {
write(",");
}
}
write("}");
endExtension();
}
}