从AJAX POST到PHP的未定义索引?

I'm new to this community and a beginner with programming. I'm trying to post a JSON data with AJAX to another PHP file and my code is as follow:

--- AJAX code ---

<?php session_start(); ?>

//other codes in between

      onApprove: function(data, actions) {
        return actions.order.capture().then(function(details) {
          //alert('Transaction completed by ' + details.payer.name.given_name);
          alert(data.orderID);

          // Call your server to save the transaction
          return fetch('/paypal-transaction-complete', {
            method: 'post',
            body: JSON.stringify({                    
              orderID: data.orderID
            })
          });

        });

        var orderid_data = data.orderID;

        $.ajax({
            url: "test_parse.php",
            type: "POST",
            data: {'order_ID': orderid_data },
            success: function(data){
                alert(data);
        });

--- PHP code ---

var_export($_POST);
echo $_POST["order_ID"];

I've realised that my $_POST return an empty array.

Any help would be greatly appreciated :)

Since you open [...]test_parse.php directly in your browser, there is no POST-data present anymore, therefor its empty.

The POST-Data is part of the request. If you open it manually in the browser you perform a GET request.

What can you do to see the var_export?

You can use Chrome or Firefox for example both have Developer-Tools which allow you to trace the exact POST-Request you performed via ajax.

For Chome:

  • Hit F12 to open the Dev Tools
  • Navigate to Network

Now perform your Ajax-Post-Request and find it in the Network list. By clicking on the requests you will see all details you will need. Most Imporant for you is Response