javascript 非常简单的 jquery post()(尝试将 html 发送到另一个页面)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9497866/
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
Very simple jquery post() (trying to send html to another page)
提问by lharby
I am struggling with the post()
method.
我正在努力使用该post()
方法。
I've been reading several posts on here and the jquery forums, and just trying to get a simple piece of code working is proving difficult. My ultimate goal is to pass a div #exportData and all it's children to test.php.
我已经在这里和 jquery 论坛上阅读了几篇文章,只是试图让一段简单的代码工作被证明是困难的。我的最终目标是将 div #exportData 及其所有子项传递给 test.php。
I started with:
我开始于:
$.post("ajax/test.php", $("body").html());
To my mind this should return the entire contents of the current page to test.php (unless test.php requires a holding div or element to receive the content). Currently it only returns the blank page.
在我看来,这应该将当前页面的全部内容返回给 test.php(除非 test.php 需要一个持有 div 或元素来接收内容)。目前它只返回空白页。
I then tried looking at the parameters for post()
if I need to manipulate these:
然后我尝试查看参数post()
是否需要操作这些参数:
$.ajax({
type: 'POST',
url: ajax/test.php,
data: data,
success: success,
dataType: dataType
});
Also declared a variable:
还声明了一个变量:
var data = {
html: #exportData
};
That bit failed of course. I don't know how to define the data in the parameters, or if this is the right place to do it.
那一点当然失败了。我不知道如何在参数中定义数据,或者这是否是这样做的正确位置。
Although I would have thought if:
虽然我会想如果:
$.post("ajax/test.php", $("body").html());
would have worked then presumably I can substitute "body" for any class, id or selector I like.
本来可以工作的,大概我可以用“body”代替我喜欢的任何类、id 或选择器。
Also does the submit button need certain parameters to tie it to the post function. At the moment it is purely:
提交按钮是否也需要某些参数才能将其与发布功能联系起来。目前它纯粹是:
<input type="submit" id="submit" value="send" name="submit">
Sorry this is such a basic question.
对不起,这是一个如此基本的问题。
采纳答案by Nicola Peluchetti
You could do
你可以做
var html = $("body").html();
var data = {
html: html
};
$.post("ajax/test.php", data);
as the second parameter of $.post() is an object wich contains the data you want to send to the server.
因为 $.post() 的第二个参数是一个对象,其中包含要发送到服务器的数据。
To send the data you could do:
要发送数据,您可以执行以下操作:
<input type="submit" id="submit" value="send" name="submit">
js
js
$('input#submit').click(function(e){
//prevent submitting the form (if there is a form)
e.preventDefault();
var html = $("body").html();
var data = {
html: html
};
$.post("ajax/test.php", data);
});
server side you receive the data
服务器端您接收数据
$html = $_POST['html']
回答by Didier Ghys
$.post()expects an object to be passed to transfert data along with the post request:
$.post()期望一个对象与 post 请求一起传递给 transfert 数据:
jQuery.post( url [, data] [, success(data, textStatus, jqXHR)] [, dataType] )
url:A string containing the URL to which the request is sent.
data: A map or string that is sent to the server with the request.
success(data, textStatus, jqXHR):A callback function that is executed if the request succeeds.
dataType:The type of data expected from the server. Default: Intelligent Guess (xml, json, script, text, html).
jQuery.post( url [, data] [, success(data, textStatus, jqXHR)] [, dataType] )
url:包含请求发送到的 URL 的字符串。
数据:随请求发送到服务器的映射或字符串。
success(data, textStatus, jqXHR):请求成功时执行的回调函数。
dataType:服务器预期的数据类型。默认:智能猜测(xml、json、脚本、文本、html)。
var content = $ ('body').html(),
data = { content: content };
$.post('ajax/test.php', data);
回答by Tx3
Sending html in the post doesn't sound like a good idea. All elements type of input
or select
will be empty in the Html. You would need to use .serialize
in order to get the values.
在帖子中发送 html 听起来不是一个好主意。Html 中的所有元素类型input
或select
将是空的。您需要使用.serialize
才能获取值。
$('#submit').submit(function () {
$.ajax({
type: 'POST',
url: 'ajax/test.php',
data: {
html: $('body').html()
}
});
});