gpt4 book ai didi

javascript - 用 phantomJS 关闭

转载 作者:行者123 更新时间:2023-12-02 15:40:02 24 4
gpt4 key购买 nike

我正在使用 phantomJS 从服务填充数据。在下面的代码中设置了 sessionToken 但我无法在函数范围之外访问它。我知道我需要使用闭包,但我无法让它工作。如何编写闭包以便可以访问作用域之外的变量?

var url = "https://securitybeta.srsplatform.com/WebServices/Srs.WebPlatform.WebServices.SrswpSite.SessionTokenService.svc/JSON/ISessionTokenServiceV2/CreateSessionToken?username=srsCore&password=[redacted]";

var page = require('webpage').create();

page.onResourceError = function(resourceError) {
console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};

var sessionToken = 0;

var getSessionToken = page.open(url, 'get', function(status) {
sessionToken = page.plainText;
sessionToken = JSON.parse(sessionToken);
sessionToken = sessionToken.Item.AuthorizedSessionToken;

console.log('Status: ' + status + ' sessionToken = ' + sessionToken);
//console.log(sessionToken);
return function () {
return sessionToken;
}

});
sessionToken = getSessionToken();
console.log('sessionToken outside of function scope: ' + sessionToken);

sessionToken将在以下代码中使用:

 var webPage = require('webpage');
var vehiclePage = webPage.create();
var settings = {
operation: "POST",
encoding: "utf8",
headers: {
"Content-Type": "application/json"
},
data: JSON.stringify({
"VIN": "JF2SJAAC5EG432990",
"StandardIds": null,
"LicenseCountry": null,
"LicensePlate": "334 142",
"LicenseState": "UT",
"OdometerUnitOfMeasure": 0,
"UnitNumber": null,
"Year": 1994,
"Make": "Chevrolet",
"Model": "Camaro",
"Engine": null,
"Color": null,
"Description": null
})
};

vehiclePage.onResourceError = function(resourceError) {
console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};

vehiclePage.open("https://ws-smsbeta.srsplatform.com/api/v1/Vehicle/Save?sessionToken=" + sessionToken + "&shopId=" + SHOP_ID, settings, function(status) {
var vehicleGuid = vehiclePage.plainText;
console.log('Status: ' + status + " " + vehicleGuid);
phantom.exit();
});

最佳答案

首先,如果要在匿名函数之外使用该变量,必须在匿名函数之前声明它。

var sessionToken = {};

page.open(url, 'get', function(status) {
sessionToken = page.plainText;
sessionToken = JSON.parse(sessionToken);
sessionToken = sessionToken.Item.AuthorizedSessionToken;

console.log('Status: ' + status + ' sessionToken = ' + sessionToken);
//console.log(sessionToken);

});

console.log('sessionToken outside of function scope: ' + sessionToken);

但是您必须了解 page.open 方法是异步的,并且上面代码的最后一行将在传递给它的匿名函数的第一行之前执行。该函数只有在获取到 url 后才会被调用,因此,您可以使用它的 .plainText

如果你尝试在函数外部使用.plainText,在调用page.open方法后,你就会遇到同样的问题,一旦它尚未加载。

<小时/>

更新

版本完成后,您必须将其他代码放入函数中,例如:

function loadVehicle(sessionToken) {
var webPage = require('webpage');
var vehiclePage = webPage.create();
var settings = {
operation: "POST",
encoding: "utf8",
headers: {
"Content-Type": "application/json"
},
data: JSON.stringify({
"VIN": "JF2SJAAC5EG432990",
"StandardIds": null,
"LicenseCountry": null,
"LicensePlate": "334 142",
"LicenseState": "UT",
"OdometerUnitOfMeasure": 0,
"UnitNumber": null,
"Year": 1994,
"Make": "Chevrolet",
"Model": "Camaro",
"Engine": null,
"Color": null,
"Description": null
})
};

vehiclePage.onResourceError = function(resourceError) {
console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
};

vehiclePage.open("https://ws-smsbeta.srsplatform.com/api/v1/Vehicle/Save?sessionToken=" + sessionToken + "&shopId=" + SHOP_ID, settings, function(status) {
var vehicleGuid = vehiclePage.plainText;
console.log('Status: ' + status + " " + vehicleGuid);
phantom.exit();
});
}

然后,在传递给 page.open 方法的匿名函数内:

page.open(url, 'get', function(status) {
sessionToken = page.plainText;
sessionToken = JSON.parse(sessionToken);
sessionToken = sessionToken.Item.AuthorizedSessionToken;
loadVehicle(sessionToken);
});

关于javascript - 用 phantomJS 关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32661640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com