node.js 関数で JSONP を使用しています。
this.send(JSON.stringify({
type: 'hello',
username: this.data('username'),
friends: friends
}));
ただし、予期しないトークン ":" エラー (json には表示されません) が表示されます。この投稿を読んだ後: 'Uncaught SyntaxError: Unexpected token : ' in jsonp
json/jsonp の問題である可能性があることがわかりました。だから私は自分のコードを次のように変更しました:
this.jsonp(JSON.stringify({
type: 'hello',
username: this.data('username'),
friends: friends
}));
ただし、これにはメソッド「jsonp」がありません。クライアント側でjsonpを使用していたため、sendも使用できません。jsonp はここ以外の場所で使用できるため、これは奇妙です。user.js ファイル内のいくつかの関数を次に示します。
User.prototype.send = function(code, message, callback) {
this._send('listener', code, message, callback);
};
User.prototype._send = function(type, code, message, callback) {
if(!message && typeof code != 'number') {
callback = message;
message = code;
code = 200;
}
if(typeof message != 'string')
message = JSON.stringify(message);
if(type == 'connection' && this.connection) {
this.connection.writeHead(code || 200, {
'Content-Type': 'application/json',
'Content-Length': message.length
});
this.connection.end(message);
} else {
if(!this.listeners.length)
return this.message_queue.push(arguments);
var cx = this.listeners.slice(), conn;
this.listeners = [];
while(conn = cx.shift()) {
conn.writeHead(code || 200, {
'Content-Type': 'application/json',
'Content-Length': message.length
});
conn.end(message);
}
if(callback) callback();
}
};
内部で send 関数を呼び出しているように見えますが、この json を jsonp に変更する場所が見つからないため、クライアント側で予期しないトークン エラーがスローされることはありません。 .