2013-12-10 5 views
2

PhantomJS를 사용하여 일부 쿠키를로드하려고하는데 일부 오류가 발생합니다.JSON의 Phantomjs에 쿠키를 추가 할 수 없습니다.

위의 코드는 다음과 같은 예외를 throw : 여기 내 코드 나는 간단한 해결책이 여기에있다 확신

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에서 문제가 발생합니까?

편집 :

{ 
    * "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 source code 대해 addCookie의 의견에 따라 형식의 QVariantMap을 전달?

답변

3

좋아 내가 그것을 알아 냈어. PhantomJS addCookie는 쿠키 형식에 대해 매우 까다 롭습니다.

기본적으로 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 
     }; 
    } 
0

당신은 실제로 다시 변수에 객체의 캐릭터 라인 버전을 할당 할 수 있습니다

temp = JSON.stringify(temp); 
+0

예, 이것을 잊어 버렸습니다. 그러나이 코드 줄을 추가하면 여전히 예외가 throw됩니다. –

관련 문제