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

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

问题描述

我尝试使用PhantomJS登录时发现了一个有趣的问题。

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 

在远程调试器中:

> 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()



<将在XHR请求后给我正确的错误的密码消息,而使用phantomjs给我一个通用的错误,因为没有cookie与phantomjs发送(我检查了头)。

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).

我很困惑为什么phantomjs不发送cookie的POST请求。有人知道我们怎么能得到phantomjs发送cookie的所有请求,因为它应该?设置cookie文件没有任何区别。

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.

推荐答案

好,这似乎与会话Cookie有关,不是常规的cookies。

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

在开发者的一个巨大的线程负责phantomjs的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

如果你不希望在整个文件中查看,基本上是:
Phantomjs行为像一个普通的浏览器,并且当浏览器关闭时,在脚本执行结束时,它会删除所有会话Cookie。

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.

因此,即使设置 - cookies-file = / path / to / cookies.txt 选项,您只能在其上存储常规Cookie,用于子执行。

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

两种可能的方法。一个是将所有请求放在同一个脚本中,另一个是手动存储和恢复cookie。

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...

您可以下载源代码和重新打开phantomjs

You could download source code and recopile phantomjs

您需要编辑src / cookiejar.cpp 并删除或注释 purgeSessionCookies();

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

希望这有助于。

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

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