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");
}
});