我需要在端口7788上创建web api,它将接受json,然后将json传递给我的api,它将呈现网站,然后幻像js将在该页面上找到元素,并且端口7788上的web api应该返回该元素。一切顺利,直到我尝试在7788上向我的服务器发送响应,错误:
无法访问已删除QObject的成员`write':
为此:firstResponse.write(imgsrc);
是否可以在调用page.open之后返回带有server.listen响应的元素?
var server = require('webserver').create();
var port = require('system').env.PORT || 7788;
server.listen(port, {'keepAlive': false}, function (request, response) {
console.log("request method: ", request.method); // request.method POST or GET
var imgsrc;
var firstResponse = response;
if (request.method == 'POST') {
var page = require('webpage').create();
var settings = {
operation: "POST",
encoding: "utf8",
headers: {
"Content-Type": "application/json"
},
data: JSON.stringify(request.post)
};
var url = "http://127.0.0.1:5000/mapapp";
page.onConsoleMessage = function(msg, lineNum, sourceId) {
console.log('CONSOLE: ' + msg + ' (from line #' + lineNum + ' in "' + sourceId + '")');
};
page.open(url, settings, function (status) {
setTimeout(function () {
page.evaluate(function () {
imgsrc = document.getElementById("exportedImg").src;
});
}, 5000);
});
page.close();
setTimeout(function () {
firstResponse.write(imgsrc);
}, 7000);
firstResponse.close();
}
});
答案 0 :(得分:1)
你调用firstResponse.close();之前调用firstResponse.write(imgsrc);超时后 将firstResponse.close()移动到setTimeout处理程序:
page.close();
setTimeout(function () {
firstResponse.write(imgsrc);
firstResponse.close();
}, 7000);