0

Kindly go through the below text below marking this as duplicate.

I am having function parameters in the ajax function and hence unable to understand how to use a callback. Please suggest a working code for the below mentioned scenario.

I am trying to retrieve a value from a JSON object obtained after parsing it from a text file received from an ajax call.

var stockInfo = passCodes(z,i,j);

Here passCodes is a function with an ajax call being made to a URL that transmits a text response.

function passCodes(arg1,arg2,arg3){// these arguments are used to build URL
alert("inside passcodes");
var json={};
$.ajax({

    type: "GET",
    url: "url.com",
    dataType: "text",
    success: function(resp) {
        resp = resp.match(/{.+}/)[0]; //to remove the garbage text
        json=JSON.parse(resp); // json object created perfectly
        return json.key1.key2;

    },
    error: function(resp) {
        alert("An error occurred while processing response");
    }


});
Sai Krishna
  • 43
  • 1
  • 2
  • 10
  • 1
    If I had a nickel for each asynchronous dupe... –  Jun 12 '18 at 15:40
  • Please check the duplicate marked question, asynchorous callbacks are tricky to understand. Give it some time, read the answers there with a little patience, you can find working code there too. – Munim Munna Jun 12 '18 at 22:08

0 Answers0