When submitting a form using AJAX, I need to pass successful data back to the submission page. The transaction is being sent over the TRON network, and the response is an array containing the transaction ID that needs to be returned to the submission page.
In PHP, the code would look something like this -
$exchange_array = array(
'success' => '1',
);
echo json_encode($exchange_array);
But now I'm struggling with returning data in JavaScript as follows -
$(".form").submit(function(e) {
var url = "submit.php";
$.ajax({
type: "POST",
url: url,
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function (data) {
// check if successful and return txid
}
});
e.preventDefault();
});
In submit.php:
const sentTx= async () => {
const tx = await tronWeb.transactionBuilder.tradeExchangeTokens(exchangeID, tokenName, tokenAmountSold, tokenAmountExpected, ownerAddress)
const signedtxn = await tronWeb.trx.multiSign(tx, privateKey, 0);
const receipt = tronWeb.trx.sendRawTransaction(signedtxn);
var txid = signedtxn.txID;
// Return txid to ajax request result
};
sentTx();
How can I successfully return the txid to the AJAX request upon completion?