Ajax POST正在发送空参数
Ajax POST is sending empty parameters
UPDATE -我已经解决了这个问题
我发现了问题,这是php.ini
中post_max_size
的属性设置为8MB。将其更改为20MB后,一切正常工作。谢谢你指出代码中的一些语法问题。
原始文章
我在一个页面的body标签中有这样的代码:
<script type = "text/javascript" >
$(document).ready(function() {
var scriptPath = "<?php echo $jobJSPath[0]; ?>";
if (scriptPath != "") {
var hasInput = "<?php echo $hasInput; ?>";
var jobExecIp = "<?php echo $jobtekerIP; ?>";
var sentToExec = "<?php echo $sentToExececution;?>";
var hasOutput = <?php echo json_encode($allOutputVarName);?>;
$.getScript(scriptPath, function(data, textStatus, jqxhr) {
var jobBatchID = "<?php echo $jobsArray[0];?>";
var jobID = "<?php echo $jobsArray[1];?>";
var jobName = "<?php echo $jobsArray[2];?>";
// execute a function inside the script has no input parameter
if (typeof hasInput !== 'undefined' && hasInput === 'no'){
// execute a function inside the script with no input parameter
var returnVar = <?php echo $newJobName; ?>;
}
// execute a function inside the script has input parameter
if (typeof hasInput !== 'undefined' && hasInput === 'yes'){
var vars = [];
// create an array of all the paths of the input variables
var arrayFromPHP = <?php echo json_encode($newAllInputVarPath);?>;
for (var i = 0; i < <?php echo sizeof($newAllInputVarPath);?>; i++) {
vars.push(JSON.parse($.ajax({type: "GET", url: arrayFromPHP[i], async: false, cache: false}).responseText));
}
// execute a function inside the script with multiple input parameter
var returnVar = <?php echo $jobsArray[2]; ?>.apply(this, vars);
}
// get the execution status
var execMessage = textStatus;
// for the jobs without any return parameter
if (hasOutput.length = 1 && hasOutput[0] === "NULL") {
var result = "No parameters are being returned";
$.ajax({
url: 'executedJobs.php',
type: 'POST',
data: {results : result, job_batch_id : jobBatchID, job_id : jobID, job_name : jobName, sentToExec : sentToExec, jobExecIp : jobExecIp, execMessage : execMessage},
cache: false
});
} else { // for the jobs with any return parameter
if (typeof returnVar != 'undefined' ) {
// this parameter is going to be posted to another page
var result = [];
var numOfOutputVar = <?php echo $jobsArray[4]; ?>;
if (Object.prototype.toString.call(returnVar) === '[object Array]') {
var countIndex = 0;
var countValue = 0;
var allValuesNoArray = false;
// check if all the returnVar values are not [object Array]
$.each(returnVar, function(index, value) {
console.log(Object.prototype.toString.call(value));
countIndex = countIndex + 1;
// check if value is not an [object Array] not an '[object String]'
if (Object.prototype.toString.call(value) !== '[object Array]' && Object.prototype.toString.call(value) !== '[object String]'){
countValue = countValue + 1;
}
});
// if all returnVar values are not [object Array] then true
if (countIndex === countValue) {
allValuesNoArray = true;
}
// if at least one returnVar value is an [object Array] then do
if (allValuesNoArray === false ) {
// if the job has more than one return variable
if (numOfOutputVar > 1) {
$.each(returnVar, function(index, value) {
result.push(JSON.stringify(value));
})
} else { // if the job has only one return variable
var allRetVarToOne = [];
$.each(returnVar, function(index, value) {
allRetVarToOne.push(value);
})
result.push(JSON.stringify(allRetVarToOne));
}
} else { // if all returnVar values are not [object Array] then do
console.log(numOfOutputVar);
// if the job has more than one return variable
if (numOfOutputVar > 1) {
$.each(returnVar, function(index, value) {
result.push(JSON.stringify(value));
})
} else { // if the job has only one return variable
result.push(JSON.stringify(returnVar));
}
}
} else {
result.push(JSON.stringify(returnVar));
}
// executes the POST if everything is ok
$.ajax({
url: 'executedJobs.php',
type: 'POST',
data: {results : result, job_batch_id : jobBatchID, job_id : jobID, job_name : jobName, sentToExec : sentToExec, jobExecIp : jobExecIp, execMessage : execMessage},
cache: false
});
} else { // executes this POST if the job execution was not successful, with no reason
var execMessage = "An unknown falure has accourd while executing, will be executed once more"
$.ajax({
type: 'POST',
data: {results : result, job_batch_id : jobBatchID, job_id : jobID, job_name : jobName, sentToExec : sentToExec, jobExecIp : jobExecIp, execMessage : execMessage},
cache: false,
url: 'executedJobs.php'
});
}
}
}).fail(function(jqxhr, settings, exception) { // executes if the getScript(scriptPath, function(data, textStatus, jqxhr) {}) faild
var execMessage = exception.message;
var result = undefined;
var jobBatchID = "<?php echo $jobsArray[0];?>";
var jobID = "<?php echo $jobsArray[1];?>";
var jobName = "<?php echo $jobsArray[2];?>";
var sentToExec = "<?php echo $sentToExececution;?>";
$.ajax({
type: 'POST',
data: {results : result, job_batch_id : jobBatchID, job_id : jobID, job_name : jobName, sentToExec: sentToExec, jobExecIp : jobExecIp, execMessage : execMessage},
cache: false,
url: 'executedJobs.php'
});
});
}
});
</script>
问题是,我在评论下的帖子"如果一切正常,执行post"发送空参数,如果我使用var returnVar = <?php echo $newJobName; ?>
或var returnVar = <?php echo $jobsArray[2]; ?>.apply(this, vars);
执行的函数有var maxNum = 500000;
和var arrayMaxSize = 500000;
。当我在POST到executedJobs.php
的参数窗口下的控制台上查看时,我在视图窗口上没有看到正确的结果,当然也没有看到executedJobs.php页面本身。这就是var returnVar
function job1() {
var notSortNumArray = [];
var notSorted1 = [];
var notSorted2 = [];
var maxNum = 500000;
var arrayMaxSize = 500000;
var minNum = 1;
// create an array with arrayMaxSize random nmber between minNum and maxNum
for (var x = 0; x < arrayMaxSize; x++) {
notSortNumArray.push(Math.floor(Math.random() * (maxNum - minNum)) + minNum);
}
// The notSorted1 is from possition 0 untill random between 0 and arrayMaxSize
notSorted1 = notSortNumArray.slice(0, Math.floor(Math.random() * notSortNumArray.length));
// The notSorted2 is from where the notSorted1 ends untill the last number form the notSortNumArray
notSorted2 = notSortNumArray.slice(notSorted1.length, notSortNumArray.length);
// job dependencies
var nextJob = "job2, job3";
var prevJob = "null";
// results
return [ notSortNumArray, arrayMaxSize, notSorted1, notSorted2 ];
}
有趣的是,对于var maxNum = 250000;
和var arrayMaxSize = 250000
,一切都工作完美,所有的结果都被发送到executedJobs.php
页进一步。
我再次希望有人能帮助我解决这个问题,因为我不知道为什么它不适合var maxNum
和var arrayMaxSize
参数的更高数字,结果就在那里,它们和一些东西正在被发送到executedJobs.php页面,但没有任何东西过来。
我知道这是很多代码,但我希望有人能帮助我解决这个问题,因为我不知道为什么它不工作
行间无分号
var scriptPath = "<?php echo $jobJSPath[0]; ?>"
var execMessage = "An unknown falure has accourd while executing, will be executed once more"
in line:
var hasOutput = <?php echo json_encode($allOutputVarName);?>;
应该改成:
var hasOutput = JSON.parse("<?php echo json_encode($allOutputVarName);?>");
- 使用AJAX POST从javascript到带有Strong参数的Rails4控制器
- ajax post在webapi中传递null参数
- 如何在 jquery post request 中发送api_key和api_secret参数
- 如何在ajax文件上传中添加额外的POST参数
- 发送具有复杂、多个参数的POST请求
- 参数使用AJAX的PHP POST不起作用
- Angular 2:如何在 GET 和 POST 请求中设置 double、float、int 和布尔类型参数
- 使用 PHP 生成的按钮生成 POST 请求,这些按钮调用带有参数的 javascript 函数
- 通过发送POST参数在ExtJS 4中显示PDF
- 如何:redactor.js imageUpload Post Extra参数
- Angular 2:HTTP Post Request,带有URL参数和正文类型参数
- 当按下浏览器后退按钮时,如何用相同的post参数激发ajax请求
- 使用表单和POST方法通过Javascript获取URL参数
- 如何通过发送GET和POST的javascript重新加载页面,并附加附加参数
- javascript$.post将参数传递到外部PHP文件
- qoxdoo:POST方法,不在URL中显示参数
- 在转发到代理目标并将响应发送回客户端之前,编辑POST参数
- 在node.js中的post方法中接收多个参数
- JavaScript 窗口重定向安全地传递参数 (POST)
- 你如何拦截xhr POST参数/POST正文