새로운 객체를 JSON으로 게시하는 긴 폴링 작업이 있지만 객체를 반환하는 업데이트를 가져 오는 동안 유효하지 않은 JavaScript 객체가있는 것처럼 보입니다. jQuery으로 분석하고 유효하지 않은 자바 스크립트 객체이기 때문에 나는에 도달 할 수없는 여기가 업데이트를 얻었다 console.log
이다.유효한 JSON 또는 JavaScript 객체
{'body': 'hello world', 'html': '\n<div class="message" id="md1f1cdab-3c3a-4dfe-b268-834aa9981dec"><b>burakdede: </b>hello world</div>\n', 'from': 'burakdede', 'id': 'd1f1cdab-3c3a-4dfe-b268-834aa9981dec'}
응답이 나오는 전에, 나는 자바 스크립트 객체로를 설정하는 eval("(" + response + ")")
을 사용하고 있습니다 . 쇼 메시지는 새 메시지를 게시하고 업데이트를받는 데 사용됩니다. 그것은 새로운 게시와 함께 잘 작동하지만, 응답이 나오면 오류가 발생합니다.
var updater = {
errorSleepTime: 500,
poll: function() {
var args = {"_xsrf": getCookie("_xsrf")};
args.listing_id = $(".action").attr("id");
$.ajax({url: "https://stackoverflow.com/a/message/updates", type: "POST", dataType: "text",
data: $.param(args), success: updater.onSuccess,
error: updater.onError});
},
onSuccess: function(response) {
try {
updater.newMessages(eval("(" + response + ")"));
} catch (e) {
updater.onError();
return;
}
updater.errorSleepTime = 500;
window.setTimeout(updater.poll, 0);
},
onError: function(response) {
updater.errorSleepTime *= 2;
console.log("Poll error; sleeping for", updater.errorSleepTime, "ms");
window.setTimeout(updater.poll, updater.errorSleepTime);
},
newMessages: function(response) {
if (!response.messages) return;
var messages = response.messages;
//console.log(messages.length, "new messages, message is :", messages);
updater.showMessage(messages);
},
showMessage: function(message) {
var existing = $("#m" + message.id);
if (existing.length > 0)return;
var node = $(message.html);
node.hide();
$("#inbox").append(node);
node.slideDown();
},
};
function newMessage(form) {
var message = form.formToDict();
var disabled = form.find("input[type=submit]");
disabled.disable();
$.postJSON("https://stackoverflow.com/a/message/new", message, function(response) {
updater.showMessage(response);
if (message.id) {
form.parent().remove();
} else {
form.find("input[type=text]").val("").select();
disabled.enable();
}
});
}
오히려 클라이언트 측에서 고정보다는 I 서버 측 코드 고정 지금 일하고있다. 문제는 적절한 JSON을 생성 할 수 없다는 것이 었습니다.
어떤 종류의 오류가 발생합니까? – FK82