jquery post成功函数不返回html页面

时间:2016-07-23 14:11:16

标签: javascript jquery

我在perl程序中有一个javascript / jquery函数,它返回一个完整的HTML页面,我想使用jquery的.post将CGI值提交给程序并重新加载页面,以便根据数据进行进一步处理提交。

考虑以下mcve:

#!/usr/bin/perl
use CGI;
my $cgivars = CGI->new;
my $data = $cgivars->param('DATA');

my $something = "This is some data.";

my $display = qq|Content-type: text/html\n\n<http><head></head><body>The data submitted was $data</body></http>|;
my $postdata = qq|Content-type: text/html\n\n
        <http><head><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script></head>
        <body>
        <script type="text/javascript">
        function postData() {
        \$.post("jqpost.cgi",{DATA:"$something"});
        }
        </script>
        Click the button to submit some data<br>
        <button value="submit" onclick="postData()">Submit</button>
        </body></html>|;

if ($data) {
    print $display;
} else {
    print $postdata;
}
end;

当我使用

$.post("jqpost.cgi",{DATA:"$something"});
firebug的控制台显示200(OK),All / HTML选项卡显示页面,因为我希望看到它返回,传递的数据按预期使用。我知道上面的代码不包含管理返回结果的函数。 所以我正在尝试使用一个函数来加载我知道正在返回的页面,使用

$.post("jqpost.cgi",{DATA:"$something"},function(data) { document.write(data);});

页面开始在浏览器中加载,但页面永远不会完成加载。 Apache的日志不会显示任何其他数据。

我可以使用什么来成功/完全显示从服务器返回的整个页面的成功函数?

1 个答案:

答案 0 :(得分:0)

您需要关闭document.opendocument.write打开文档。

document.write("Hello there!!!");
document.close();

不再旋转。