phantomjs - 没有为任何 XHR/POST/GET AJAX 请求发送 Cookie [英] phantomjs - Cookie is not being sent for any XHR/POST/GET AJAX requests

查看:26
本文介绍了phantomjs - 没有为任何 XHR/POST/GET AJAX 请求发送 Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I found an interesting issue when attempting to login using PhantomJS. I'm at a loss as to why it's actually occurring.

Basically you start up a remote debugger like so:

/usr/local/bin/phantomjs --web-security=no --remote-debugger-port=13379 --remote-debugger-autorun=yes /tmp/test.js 

Within the remote debugger:

> location.href = "https://www.mysite.com/login"
> $('input[name="username_or_email"]').val('blah@email.com')
> $('input[name="password"]').val('wrongpassword')

> $('button[type="submit"]').submit()

Doing this in Chrome will give me the correct "wrong password" message after the XHR request, whereas using phantomjs gives me a generic error as no cookie is sent with phantomjs (I examined the headers).

I'm quite confused on why phantomjs doesn't send the cookie with the POST request. Does anyone know how we can get phantomjs to send the cookie with ALL requests as it should? Setting a cookie-file doesn't make any difference either.

解决方案

Ok, this seems to be something related with session cookies and not regular cookies.

Heres a huge thread on the developer in charge of the cookies feature of phantomjs and some guys with the same issue as yours.

https://groups.google.com/forum/#!msg/phantomjs/2UbPkIibnDg/JLV9jBKxhIQJ

If you dont want to skirm through the entire file, basically: Phantomjs behaves like a regular browser, and it deletes all session cookies when browser closes, in your case, when your script execution ends.

So, even if you set the --cookies-file=/path/to/cookies.txt option you will only store regular cookies on it, for subsecuent executions.

There are two possible approaches for you. One, is to make all requests within the same script, or the other is to store and restore the cookies manually.

On the thread there are a couple of functions that you can use to do this.

function saveCookies(sessionCookieFile, page) {
    var fs = require("fs");
    fs.write(sessionCookieFile, JSON.stringify(page.cookies));
}

function restoreCookies(sessionCookieFile, page) {
    var fs = require("fs");
    var cookies = fs.read(sessionCookieFile);
    page.cookies = JSON.parse(cookies);
}

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

And, if everything fails...

You could download source code and recopile phantomjs

You will need to edit src/cookiejar.cpp and remove or comment purgeSessionCookies();

Hope, this helps.

这篇关于phantomjs - 没有为任何 XHR/POST/GET AJAX 请求发送 Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆