Javascript jQuery 获取帖子数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3257277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery get post data
提问by betacar
Is there any way to get POST values in jQuery?
有没有办法在 jQuery 中获取 POST 值?
回答by meder omuraliev
jQuery is for client-side Javascript. You grab POST values with server-side languages. You can provide them by mixing server-side with client-side:
jQuery 用于客户端 Javascript。您可以使用服务器端语言获取 POST 值。您可以通过混合服务器端和客户端来提供它们:
<script>
(function() {
var x = "<?php echo ( isset( $_POST['name'] ) && $_POST['name'] != '') ? $_POST['name'] : '';?>";
})();
</script>
回答by gprasant
$("form").serializeArray();
回答by rfunduk
No, you could use whatever your back-end is to write some hidden fields on the page or write out a javascript script tag with the data, though.
不,您可以使用任何后端在页面上编写一些隐藏字段,或者用数据写出一个 javascript 脚本标记。
回答by Mark Schultheiss
If you use AJAX methods to post you can send back the values or capture them prior to posting via the AJAX.
如果您使用 AJAX 方法发布,您可以在通过 AJAX 发布之前发回值或捕获它们。
回答by Mariusz Jamro
Use serializeArray on your form element http://api.jquery.com/serializeArray/.
在表单元素http://api.jquery.com/serializeArray/上使用 serializeArray 。
回答by Sean Vieira
To listen for POST values from another page? Not as far as I know. You can however submit a POST request via ajax to a server-side script and have the server side script return the previously POST-ed values (if you store them in the user's session, for example).
从另一个页面监听 POST 值?据我所知没有。但是,您可以通过 ajax 向服务器端脚本提交 POST 请求,并让服务器端脚本返回先前 POST 处理的值(例如,如果您将它们存储在用户的会话中)。
回答by profimedica
POST data is sent in the body of the request, usualy in a application/x-www-form-urlencoded format.
POST 数据在请求正文中发送,通常采用 application/x-www-form-urlencoded 格式。
The last line here is what you want:
这里的最后一行是你想要的:
POST /path/script.cgi HTTP/1.0
From: [email protected]
User-Agent: HTTPTool/1.0
Content-Type: application/x-www-form-urlencoded
Content-Length: 36
[email protected]&user+name=we
You can access the request body from PHP as $_POST. If the body is not in the format that you are expected you can try:
您可以从 PHP 以 $_POST 的形式访问请求正文。如果正文不是您期望的格式,您可以尝试:
$postdata = file_get_contents("php://input"); $_POST = json_decode($postdata);
From server side you inject the content you need in your javascript:
从服务器端,您在 javascript 中注入您需要的内容:
<script>
var x = "<?php echo ( isset( $_POST['email'] ) ) ? $_POST['email'] : '[email protected]';?>";
</script>
If you still need to transport data from one page to another .html page and avoid .php files, you need to save your data before redirecting to the next page and than you can recover your data by reading the same storage.
如果您仍然需要将数据从一个页面传输到另一个 .html 页面并避免使用 .php 文件,则需要在重定向到下一页之前保存数据,然后您可以通过读取相同的存储来恢复数据。
So, use your database to keep the application state while navigating among pages.
因此,在页面之间导航时,请使用您的数据库来保持应用程序状态。

