[英]jQuery.post() dynamically generated data to server returns empty response
I'm generating a series of variables in a loop (using JS), and I'm assigning them an .id
and a .name
based on the current index. 我在循环中生成一系列变量(使用JS),并且我根据当前索引为它们分配.id
和.name
。 At each loop I'm sending a request to the server using jQuery.post()
method, but the returning response is just an empty variable. 在每个循环中,我使用jQuery.post()
方法向服务器发送请求,但返回的响应只是一个空变量。 Here's the code: 这是代码:
JavaScript JavaScript的
for ( var index = 0; index < 5; index++ ) {
var myVar = document.createElement('p');
myVar.id = 'myVarID' + index;
myVar.name = 'myVarName' + index;
//Send request to server
$(document).ready(function(){
var data = {};
var i = 'ind';
var id = myVar.id;
var name = myVar.name;
data[id] = name;
data[i] = index;
$.post("script.php", data, function(data){
console.log("Server response:", data);
});
});
}
PHP PHP
<?php
$index = $_POST['ind'];
$myVar = $_POST['myVarID'.$index];
echo $myVar;
?>
Response: Server response: ''
响应: Server response: ''
If I instead set a static index in JS code, getting rid of the loop, so for example: 如果我在JS代码中设置静态索引,摆脱循环,例如:
var index = 0;
I get the expected result: Server response: myVarName0
我得到了预期的结果: Server response: myVarName0
Why is this happening? 为什么会这样? And how can I solve it? 我该如何解决?
Assuming the php file is in order. 假设php文件是有序的。 I use this: 我用这个:
function doThing(url) {
getRequest(
url,
doMe,
null
);
}
function doMe(responseText) {
var container = document.getElementById('hahaha');
container.innerHTML = responseText;
}
function getRequest(url, success, error) {
var req = false;
try{
// most browsers
req = new XMLHttpRequest();
} catch (e){
// IE
try{
req = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
// try an older version
try{
req = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
return false;
}
}
}
if (!req) return false;
if (typeof success != 'function') success = function () {};
if (typeof error!= 'function') error = function () {};
req.onreadystatechange = function(){
if(req .readyState == 4){
return req.status === 200 ?
success(req.responseText) : error(req.status)
;
}
}
var thing = "script.php?" + url;
req.open("GET", thing, true);
req.send(null);
return req;
}
then use it like this: 然后像这样使用它:
doThing("myVarID="+myVar.id+"&i="+index);
also, you will have to change your PHP to something like this: 另外,您必须将PHP更改为以下内容:
<?php
$index = $_GET['ind'];
$myVar = $_GET['myVarID'.$index];
echo $myVar;
?>
Obviously this code needs to be edited to suit your own needs 显然,需要编辑此代码以满足您自己的需要
the function doMe is what to do when the webpage responds, in that example I changed the element with the id hahaha to the response text. 函数doMe是在网页响应时要做的事情,在该示例中,我将具有id hahaha的元素更改为响应文本。
This won't win you any prizes but it'll get the job done. 这不会赢得任何奖品,但它会完成工作。
Solution 解
It is working fine removing: 它工作正常删除:
$(document).ready()
Working code 工作代码
for ( var index = 0; index < 5; index++ ) {
var myVar = document.createElement('p');
myVar.id = 'myVarID' + index;
myVar.name = 'myVarName' + index;
//Send request to server
var data = {};
var i = 'ind';
var id = myVar.id;
var name = myVar.name;
data[id] = name;
data[i] = index;
$.post("script.php", data, function(data){
console.log("Server response:", data);
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.