0

The following function should display an alert message with the value it gets from the JSON file.

function ajaxTest() {
    let currencyRate = "";

    $.ajax({
        url: "http://free.currencyconverterapi.com/api/v5/convert?q=USD_INR&compact=y",
        dataType: "jsonp",
        success: function(json) {
            let myObject = JSON.parse(json);
            currencyRate += myObject.USD_INR.val;
        }
    })

    alert(currencyRate);
}

The json file looks like the following.

{"USD_INR":{"val":72.037972}}

The code isn't working. It's showing a blank alert box so the ajax portion isn't correct.

AmerllicA
  • 29,059
  • 15
  • 130
  • 154
NoobCoder
  • 189
  • 1
  • 14

2 Answers2

1

It is a async function call, you need to put alert in your ajax success function

function ajaxTest() {
    let currencyRate = "";

    $.ajax({
        url: "http://free.currencyconverterapi.com/api/v5/convert?q=USD_INR&compact=y",
        dataType: "jsonp",
        success: function(json) {
            let myObject = JSON.parse(json);
            currencyRate += myObject.USD_INR.val;

            // put alert here
            alert(currencyRate);
        }
    })
}

Or a better approach would be like below.

ajaxTest().then(function( json ) {
   let myObject = JSON.parse(json);
   currencyRate += myObject.USD_INR.val;

   // put alert here
   alert(currencyRate);
});

Or even use callbacks as follows:

function ajaxTest( callback ) {
        let currencyRate = "";

        $.ajax({
            url: "http://free.currencyconverterapi.com/api/v5/convert?q=USD_INR&compact=y",
            dataType: "jsonp",
            success: function(json) {
                let myObject = JSON.parse(json);
                currencyRate += myObject.USD_INR.val;


                callback(currencyRate);
            }
        })
    }

ajaxTest(function( result ) {
  alert(result)
})

Updated

No need to parse the return json at all.

function ajaxTest() {
    let currencyRate = "";

    $.ajax({
        url: "http://free.currencyconverterapi.com/api/v5/convert?q=USD_INR&compact=y",
        dataType: "jsonp",
        success: function(json) {
            // no need to parse it to json anymore
            // let myObject = JSON.parse(json);
            currencyRate += json.USD_INR.val;

            // put alert here
            alert(currencyRate);
        }
    })
}

ajaxTest();

The above code should work, since the return json itself is a json and no need to parse it to json anymore.

sadrzadehsina
  • 1,331
  • 13
  • 26
0

There are a few things wrong with your current code

  1. The dataType should be json instead of jsonp.
  2. There's no need to re-parse the success response (json) as jQuery ajax handles it for you all you need to do is to just iterate over it.

So Your current code will be as follows

function ajaxTest() {
  let currencyRate = "";

  $.ajax({
    url: "http://free.currencyconverterapi.com/api/v5/convert?q=USD_INR&compact=y",
    dataType: 'json',
    success: function(json) {
      currencyRate += json.USD_INR.val;
      console.log(currencyRate)
      alert(currencyRate);
    }
  })
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<button onClick="ajaxTest()">Get Rates</button>
Dev Man
  • 2,114
  • 3
  • 23
  • 38