2016-08-13 52 views
0

更新 - 我已經解決了這個問題阿賈克斯POST正在發送空參數

我發現這個問題,它是在php.inipost_max_size其設置爲8MB的財產。將其更改爲20MB後,所有工作都應如此。感謝您指出代碼中的一些語法問題。

原帖

我有一個頁面的主體標記驗證碼:

<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> 

的問題是,我的評論下交「執行後,如果一切正常」發送空參數如果我正在使用var returnVar = <?php echo $newJobName; ?>或var returnVar = <?php echo $jobsArray[2]; ?>.apply(this, vars);執行的功能有var maxNum = 500000;var arrayMaxSize = 500000;。當我在POST的參數窗口下查看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頁面,但沒有結果。

我知道這是很多代碼,但我希望有人可以幫我解決這個問題,因爲我不知道爲什麼它不工作。

+0

你嘗試檢查您的控制檯中的錯誤? –

+0

@PraveenKumar沒有任何錯誤,我看到'console.log(result)'的輸出,它是正確的,它只是沒有通過POST發送,如果我上面提到的數字太大 – TalG

+0

請把它分解爲一個[最小工作示例](http://stackoverflow.com/help/mcve)。 – Jeff

回答

0

沒有分號線:

var scriptPath = "<?php echo $jobJSPath[0]; ?>" 
var execMessage = "An unknown falure has accourd while executing, will be executed once more" 
符合

var hasOutput = <?php echo json_encode($allOutputVarName);?>; 

您應將其更改爲:

var hasOutput = JSON.parse("<?php echo json_encode($allOutputVarName);?>"); 
+2

還有更多沒有分號的行 –

+0

JSON。解析不需要那裏它實際上會產生一個錯誤,我只使用json_encode得到正確的輸入,我已經添加了所有缺少的「;」 :)。 – TalG

+2

您實際上不需要JavaScript中的分號,除非您希望每行運行更多語句。我建議使用它們,不管什麼tho:P –