Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
86 views
in Technique[技术] by (71.8m points)

javascript - not able to pass array from JS to PHP with AJAX

Hello am trying to pass an array from my JS script file to another file called serverSide.php so i can print the array in that file and do something with it , am having no issue sending the data and getting a response using the AJAX/JSON the problem i am getting "Notice: Undefined index: theArray in C:xampphtdocsAjax testserverSide.php on line 2" when i try to print_r the array i sent ?

**Here is my JS code **

 <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script
    src="https://code.jquery.com/jquery-3.5.1.min.js"
    integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
    crossorigin="anonymous"></script>
</head>
<body>
    <h1>Testing AJAX techniques</h1>
<script>
    let myArray = [1,2,3];
    
    console.log(myArray);
    let myArray1 = JSON.stringify(myArray);
    console.log(myArray1);

    $.post({
      method: 'POST',
      url: 'serverSide.php',
      data: {theArray: myArray1},
      success: function(res) {
        console.log(res);
      }
    })
       
</script>
</body>
</html>

And this is the serverSide.php file code where i try to print the array

    <?php 
     $test = json_decode($_POST['theArray']);
     print_r($test);
?>

Thank you for any help

question from:https://stackoverflow.com/questions/65858727/not-able-to-pass-array-from-js-to-php-with-ajax

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...