javascript - 无法从 JSON 在 Phantomjs 中添加 cookie

标签 javascript json cookies phantomjs

我正在尝试使用 PhantomJS 动态加载一些 cookie,但遇到一些错误。这是我的代码:

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

var cookieJson = require('cookie.json'); // local cookie file

phantom.cookiesEnabled = true; // Enable Cookies

phantom.clearCookies(); // Clear Old Cookies

for(var i = 0; i< cookieJson.length; i++) { //for each domain, try to add the cookie
    var temp = cookieJson[i];
    console.log(JSON.stringify(temp));  // This seems to print just fine
    phantom.addCookie(temp); // this throws an exception
    }


phantom.exit();

上面的代码抛出以下异常:

incompatible type of argument(s) in call to addCookie(); candidates were
    addCookie(QVariantMap)

我确信这里有一个简单的解决方案,但我的大脑卡住了。我的印象是 JSON.stringify 从 JSON 对象返回一个字符串。真正令人困惑的是,当我将其打印到控制台时,它看起来与我将其存储为 String 完全相同。我的数据如下所示:

{"domain": ".sweatytacobo.com",
"expires": "Tue, 10 Jun 2014 16:37:46 GMT",
"expiry": ,
"httponly": false,
"name": "__utmz",
"path": "/",
"secure": false,
"value": "268881515.13222266.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)"}

当我将上面的内容用作字符串时,它的添加没有问题。那么为什么我的 JSON.Stringify 会给我带来问题呢?

编辑:

根据PhantomJS source code的评论addCookie 以以下格式传递 QVariantMap:

 {
     *   "name"     : "cookie name (string)",
     *   "value"    : "cookie value (string)",
     *   "domain"   : "cookie domain (string)",
     *   "path"     : "cookie path (string, optional)",
     *   "httponly" : "http only cookie (boolean, optional)",
     *   "secure"   : "secure cookie (boolean, optional)",
     *   "expires"  : "expiration date (string, GMT format, optional)"
     * }

那么我不应该能够以某种方式传递 JSON 对象吗?

最佳答案

好吧,我明白了。 PhantomJS addCookie 对 cookie 的格式非常挑剔。

基本上,为了转换 JSON,您必须通过迭代 JSON 对象来提取值。例如:

var cookieJson = require('cookiefile.json'); 
// load in the cookies in JSON format from file

for(var i = 0; i< cookieJson.length; i++) {
    var tempCookie = {}; 

    tempCookie["name"] = cookieJson[i]["name"];
    tempCookie["domain"] = cookieJson[i]["domain"];
    tempCookie["value"] = cookieJson[i]["value"];
    tempCookie["path"] = cookieJson[i]["path"];
    tempCookie["session"] = cookieJson[i]["session"];
    tempCookie["storeId"] = cookieJson[i]["storeId"];

    // Here we are adding the relevant values as needed and in the proper format

    var tempADD = {"domain": tempCookie["domain"],
    "expires": tempCookie["expires"],
    "expirationDate": 1402418266,
    "httpOnly": tempCookie["httpOnly"],
    "name": tempCookie["name"],
    "path": tempCookie["path"],
    "secure": tempCookie["secure"],
    "value": tempCookie["value"],
    "session": tempCookie["session"],
    "storeId": tempCookie["storeId"]};

    // Finally, we add the cookie. phantom.addCookie returns true if successful
    if(phantom.addCookie(tempADD)){
        console.log("Cookie Added!");
        } else {
        console.log("Cookie Failure: ");
        console.log(JSON.stringify(tempADD)) // print the cookie which failed to load
        };
    }

关于javascript - 无法从 JSON 在 Phantomjs 中添加 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20487028/

相关文章:

cookies - Golang中如何查看cookieJar的内容?

javascript - 设置 &lt;textarea&gt; 值时不解码 HTML 实体

javascript - Ctr + 单击在 ie11 中不起作用

javascript - 从脚本安装 Google Chrome 扩展

javascript - 将 csv 转换为 json 返回空 json 文件

python - 如何对json文件进行切片,只提取部分字段

javascript - 如何解析通过 ajax 请求发送的多部分/表单数据。?

arrays - 在循环中将变量添加到 bash 数组

javascript - Safari 不向第二个 CORS 请求包含 COOKIE

c# - 如何在 asp.net c# 中从浏览器中删除 cookie,所以答案不工作