在 jQuery 中提交页面之前抓取 HTML
我需要获取当前页面的 HTML 并通过表单提交。我的页面的简化版本如下所示:
<body>
<div id="container">
<!-- stuff here -->
<form id="pageform" action="mypage.php" method="post">
<input type="hidden" id="Source" />
<button type="submit" name="submit">Submit</button>
</form>
</div>
<script type="text/javascript">
function handlesubmit() {
var pageSource = $("#container").html();
$("#Source").val(pageSource);
return true;
}
$(function() {
$("#pageform").submit(handlesubmit);
});
</script>
</body>
此代码在 Internet Explorer 9、Firefox 和 Chrome 中运行良好。不过,在 Internet Explorer 7 和 Internet Explorer 8 中,Source
的结果值在服务器端被截断(源相当大)。我可以通过更改通过 $.post()
进行表单提交来解决此问题。
为什么使用 jQuery Ajax post 而不是常规的表单提交可以实现这一点?在设置 Source
的值之前提交表单是否会导致竞争条件?上述代码的哪些部分是异步发生的 - 仅 $.post()
请求?
更新:请求是通过 POST 执行的,而不是 GET。
I need to grab the HTML of the current page and submit it via a form. A simplified version of my page looks like this:
<body>
<div id="container">
<!-- stuff here -->
<form id="pageform" action="mypage.php" method="post">
<input type="hidden" id="Source" />
<button type="submit" name="submit">Submit</button>
</form>
</div>
<script type="text/javascript">
function handlesubmit() {
var pageSource = $("#container").html();
$("#Source").val(pageSource);
return true;
}
$(function() {
$("#pageform").submit(handlesubmit);
});
</script>
</body>
This code works fine in Internet Explorer 9, Firefox and Chrome. In Internet Explorer 7 and Internet Explorer 8, though, the resulting value of Source
is truncated on the server-side (the source is fairly large). I was able to fix this by changing the form submission to take place via $.post()
instead.
Why does this work using a jQuery Ajax post and not regular form submission? Is there a race condition caused by a the form submitting before the value of Source
is set? Which parts of the above code happen asynchronously - only the $.post()
request?
UPDATE: The request is performed via POST, not GET.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,没有竞争条件。查询字符串的长度受到限制。如果您要发送大量数据,那么您应该使用 POST 方法(它将数据放入请求正文中)。
No, there is no race condition. The query string is simply limited in length. If you are sending large amount of data, then you are supposed to use the POST method (which will put data into the request body).