[英]How to POST using jquery and capture the reply in JSON
I am using this code: 我正在使用此代码:
function save() {
// submit the dataform
$.post(document.dataform.action,
$("#dataform").serialize(),
function(reply) {
//handle reply here
});
}
This sends the right data to the server, but it arrives in $_GET. 这会将正确的数据发送到服务器,但到达$ _GET。 When I alter the server code to match I get the expected reply. 当我更改服务器代码以使其匹配时,会得到预期的答复。 There is a part of the query on the dataform.action. 在dataform.action上有一部分查询。 which I expected to arrive in $_GET. 我预计会到达$ _GET。
How can I actually get the POST to send the data from the form so it arrives in $_POST, and thus avoid the size restrictions on GET? 如何实际获取POST以从表单发送数据,使其到达$ _POST,从而避免GET的大小限制?
I'm testing with Firefox, JQuery 9, and PHP 5.4.3 我正在使用Firefox,JQuery 9和PHP 5.4.3进行测试
Thanks, Ian 谢谢,伊恩
$_GET takes parameter from query string so to have $_POST and $_GET just do this: $ _GET从查询字符串中获取参数,因此只需$ _POST和$ _GET即可:
var action = document.dataform.action;
var get_variable = "var1=v1&var2=v2...";
action = action+"?"+get_variable;
$.post(action,
$("#dataform").serialize(),
function(reply) {
//handle reply here
});
function save() {
// submit the dataform
$.post(document.dataform.action,
{ data: $("#dataform").serializeArray() })
.done(function(reply) {
//handle reply here
});
}
then json_decode($_POST['data']); 然后json_decode($ _ POST ['data']); in PHP 在PHP中
Cracked it! 破解了! The correct method is 正确的方法是
function save() {
// submit the dataform
$.ajax({
url: document.dataform.action,
data: new FormData(document.dataform),
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function(reply){
if (reply.action) fetch('/content.php',reply.action);
if (reply.content) document.getElementById('content').innerHTML=reply.content;
if (reply.menu) document.getElementById('menu').innerHTML=reply.menu;
if (reply.status) document.getElementById('status').innerHTML=reply.status;
calcSize();
}
});
}
This will however only work with browsers that support FormData - Chrome 7+, FF4.0+, IE 10+, Opera 12+ and Safari 5+ 但是,这仅适用于支持FormData的浏览器-Chrome 7 +,FF4.0 +,IE 10 +,Opera 12+和Safari 5+
OK for my use-case ;) Thanks for everyone's input. 我的用例还可以;)谢谢大家的投入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.