Jquery Primefaces回调使用'&';失败
在测试期间,如果带有JSON数据的HTTP响应包含“&”,primefaces将失败,如果我删除了“&”,它将正常工作。我发送的数据是一个URL,我是否需要以编码格式发送URL?如果任何数据(不是url)包含“amp”或任何此类字符,是否会发生此问题?在这种情况下,最好的方法是什么 带回调的commandlink 谷歌开发者工具包的回应Jquery Primefaces回调使用'&';失败,jquery,json,primefaces,Jquery,Json,Primefaces,在测试期间,如果带有JSON数据的HTTP响应包含“&”,primefaces将失败,如果我删除了“&”,它将正常工作。我发送的数据是一个URL,我是否需要以编码格式发送URL?如果任何数据(不是url)包含“amp”或任何此类字符,是否会发生此问题?在这种情况下,最好的方法是什么 带回调的commandlink 谷歌开发者工具包的回应 <partial-response><changes><update id="javax.faces.ViewState">
<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>
目前(v.5.2)primefaces部分响应编写器没有为XML转义JSON,这会导致字符“”和“&”等出现问题
为了让它工作,我修补了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();
}
}
private void encodeCallbackParams(映射参数)抛出IOException、jsoneexception{
if(params!=null&&!params.isEmpty()){
startExtension(回调扩展参数);
写(“{”);
for(Iterator it=params.keySet().Iterator();it.hasNext();){
字符串paramName=it.next();
对象paramValue=params.get(paramName);
if(JSONObject的参数值实例){
字符串json=((JSONObject)paramValue).toString();
json=XML.escape(json);
写(\);
写(名字);
写(“\”:{”);
写入(json.substring(1,json.length()-1));
写(“}”);
}else if(JSONArray的参数值instanceof){
字符串json=((JSONArray)paramValue).toString();
json=XML.escape(json);
写(\);
写(名字);
写(“\”:[”);
写入(json.substring(1,json.length()-1));
填写(“]”);
}else if(isBean(paramValue)){
String json=新的JSONObject(paramValue).toString();
json=XML.escape(json);
写(\);
写(名字);
写(“\”:”;
写入(json);
}否则{
String json=new JSONObject().put(paramName,paramValue).toString();
json=XML.escape(json);
写入(json.substring(1,json.length()-1));
}
if(it.hasNext()){
写(“,”;
}
}
写(“}”);
endExtension();
}
}
你的失败对我来说很好:@pb2q你是对的json解析器没有问题,primefaces由于某种原因无法解析它,因为数据是通过它来的。似乎反斜杠才是问题所在。请参见此处:@Heidarzadeh Primefaces无法发送响应,如果它从服务器端发送为“&”。当我从前端调试它时,“状态;的响应显示为parseException。这是primefaces给出的问题。如果你用一个额外的参数修补它(因此默认行为是向后兼容的),你可以在github中提交一个pull请求。。。那对大家都有好处!这个问题已经向primefaces报告,但可能需要一段时间才能包含在下一个版本中…这就是为什么我提到要创建一个完整的补丁和拉请求,如上所述。这将加速事情的发展……谷歌目前的PF发行列表是不够的
<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
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();
}
}