2

So I am trying to create my own BlockChain website using NXT API. They have listed the possible operations you can make on their website but there is no source code at all. After hours searching I found this project on github, It's the NXT API implemented in Node.js and also he explains how to use it in the browser I managed to make it work on my website but many functions of his API are not updated and didn't work for me. So I had to do it from scratch based on the php example from NXT page.

I am going to explain how I am trying to run that php function from my HTML page

Following the answer of calling a PHP function with AJAX, I created a PHP file called getGuaranteedBalance.php like this:

 <?php
    header('Content-Type: application/json');

    $aResult = array();

    if( !isset($_POST['functionname']) ) { $aResult['error'] = 'No function name!'; }

    if( !isset($_POST['arguments']) ) { $aResult['error'] = 'No function arguments!'; }

    if( !isset($aResult['error']) ) {

        switch($_POST['functionname']) {
            case 'get_guaranteed_balance':
               if( !is_array($_POST['arguments']) || (count($_POST['arguments']) < 2) ) {
                   $aResult['error'] = 'Error in arguments!';
               }
               else {
                   $server= $_POST['arguments'][0];
                   $accno= $_POST['arguments'][1];
                   $number_of_confirmations= $_POST['arguments'][2];


                   $command = "nxt?requestType=getGuaranteedBalance&account=$accno&numberOfConfirmations=$number_of_confirmations";
                   $data = file_get_contents($server . $command);
                   // example results: {"guaranteedBalance":2700000}
                   $obj = json_decode($data);
                   $guaranteed_balance = $obj->{'guaranteedBalance'};  // nxt cents
                   $guaranteed_balance = floatval($guaranteed_balance) / 100.0; // nxt
                   $aResult['result'] = $guaranteed_balance;
               }
               break;

            default:
               $aResult['error'] = 'Not found function '.$_POST['functionname'].'!';
               break;
        }

    }

    echo json_encode($aResult);

?>

And this is how I call it using AJAX from my HTML page:

<script language="javascript">      
jQuery.ajax({
    type: "POST",
    url: 'Functions/getGuaranteedBalance.php',
    dataType: 'json',
    data: {functionname: 'get_guaranteed_balance', arguments: ["http://localhost:6876", "2488827424806206243", 50]},

    success: function (obj, textstatus) {
            console.log("Hii "+obj.result.toString());
            if( !('error' in obj) ) {
                console.log("Hiiiiiiiii"+obj.result.toString());
            } else {
                console.log(obj.error);
            }
    }
});     
</script>

On the console, the only message I get is: XHR finished loading: POST "http://localhost:6876/getGuaranteedBalance.php" jquery.min.js:4 actually it should return the balance value which is 974400000000.

I have tried everything changing the parameter to data instead of variables obj or adding alerts everywhere but nothing worked. When I access from browser doing the request manually the request works which means the problem is in the code...

REQUEST: http://localhost:6876/nxt?requestType=getGuaranteedBalance&account=2488827424806206243&numberOfConfirmations=60

It would be really helpful if someone has a better Idea of implementing this API or if you know one already implemented please share it.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Programmer Man
  • 1,314
  • 1
  • 9
  • 29

0 Answers0