HTTP POST请求显示HTML错误,而不是在请求正文中显示错误

HTTP POST请求显示HTML错误,而不是在请求正文中显示错误,post,coffeescript,phantomjs,casperjs,Post,Coffeescript,Phantomjs,Casperjs,我正在使用casperjs发布到URL,并使用fiddler2调试我的代码。下面是我的代码(用coffeescript编写) 当我运行它时,我得到了以下调试信息: C:\casperjs\batchbin\cj.bat C:\Users\***\WebstormProjects\haha\test.coffee [info] [phantom] Starting... [info] [phantom] Running suite: 2 steps [debug] [phantom] openin

我正在使用
casperjs
发布到URL,并使用
fiddler2
调试我的代码。下面是我的代码(用
coffeescript
编写)

当我运行它时,我得到了以下调试信息:

C:\casperjs\batchbin\cj.bat C:\Users\***\WebstormProjects\haha\test.coffee
[info] [phantom] Starting...
[info] [phantom] Running suite: 2 steps
[debug] [phantom] opening url: http://www.sample.com/test, HTTP POST
[debug] [phantom] Navigation requested: url=http://www.sample.com/test, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "http://www.sample.com/test"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/2 http://www.sample.com/test (HTTP 200)
<html xmlns="http://www.w3.org/1999/xhtml"><body><parsererror style="display: block; white-space: pre; border: 2px solid #c77; padding: 0 1em 0 1em; margin: 1em; background-color: #fdd; color: black"><h3>This page contains the following errors:</h3><div style="font-family:monospace;font-size:12px">error on line 1 at column 1: Start tag expected.</div><h3>Below is a rendering of the page up to the first error.</h3></parsererror></body></html>
[info] [phantom] Step anonymous 2/2: done in 657ms.
[info] [phantom] Done 2 steps in 675ms

Process finished with exit code 0
我的回复是:

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/xml; charset=utf-8
Vary: Accept-Encoding
Date: Mon, 08 Dec 2014 14:36:34 GMT
Content-Length: ***

http://www.sample.com/this_is_a_sample_url
请注意,URL中的响应正文。但是
getPageContent()
给了我一段html代码。首先,我认为问题可能是由
Accept
请求头引起的。但是,它已经被设置为
text/plain
,而不是HTML。
有人能给我一些建议吗?

您的服务器的响应可能包含正确的数据,但由于它以XML形式返回URL,PhantomJS无法正确解析。这就是为什么会有一个错误显示为HTML页面

您应该专门使用下载内容(在JavaScript中):

在开始时,会打开一个虚拟(现有)页面以正确初始化系统。当虚拟域与实际请求域不同时,还需要使用命令行选项运行CasperJS


如果在
start
上使用虚拟本地html文件,则应添加命令行选项,因为当前URL是关于:blank,AJAX请求仅限于当前域。

Hi Artjom,我应该将这段代码放在哪里?我把它放在顶层,得到了“CasperError:没有定义步骤,正在中止”。还有一种方法可以指定自定义HTTP请求头吗?我只需要发布到一个URL,不需要其他步骤,所以我认为我不能在这个URL上启动
start
。在
then
块中,我还忘记了必要的命令行选项。请参阅我的更新答案。我使用的代码与您提供的一样,并且在获得返回值时使用
console.log(content)
。我看不到小提琴手的请求。我错过什么了吗?我得到的是:[信息][幻影]开始。。。[info][phantom]运行套件:1步[debug][phantom]成功注入Casper客户端实用程序null[info][phantom]步骤匿名1/1:在28毫秒内完成。[info][phantom]在44毫秒的过程中完成了1个步骤,退出代码为0。请试用。这本应该奏效的。我找到了一个解决方法。我也对它做了一些修改,使它现在可以重复使用。
POST http://www.sample.com/test HTTP/1.1
Origin: null
Content-Length: ***
Accept: text/plain, */*
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36777
Cookie: ***
Connection: Keep-Alive
Accept-Encoding: gzip
Accept-Language: en-US,*
Host: www.sample.com

a=aaa&b=bbb&c=ccc
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/xml; charset=utf-8
Vary: Accept-Encoding
Date: Mon, 08 Dec 2014 14:36:34 GMT
Content-Length: ***

http://www.sample.com/this_is_a_sample_url
casper.post = function(url, data){
    return this.evaluate(function(targetURL){
        return __utils__.sendAJAX(targetURL, "POST", data, false, {
            overrideMimeType: "text/plain"
        });
    }, url);
};

casper.start("http://example.com").then(function(){
    var content = this.post(targetURL, {
        a     : "aaa",
        b     : "bbb",
        c     : "ccc"
    });
    // do something with content
}).run();