Reputation: 305
i'm using ajax to get some data which i want to use after ajax is done. however i can't manipulate the response here's my code
function testAjax() {
direc = "controller.php";
return $.ajax({url: direc});
}
var obj = testAjax();
console.log(obj);
console.log(obj.responseText);
console.log(obj) returns this :
XMLHttpRequest { readyState=1, timeout=0, withCredentials=false, more...}
response
"[{"CLICREDIT_Cod":"1002","CLICREDIT_Credit":"2000","CLICREDIT_FlagEstad":"1"}]"
responseText
"[{"CLICREDIT_Cod":"1002","CLICREDIT_Credit":"2000","CLICREDIT_FlagEstad":"1"}]"
console.log(obj.responseText) returns this :
(an empty string)
is there a way to manipulate that data? i've tried some stuff like JSON.parse but nothing seems to work, any help would be appreciated. thanks
Upvotes: 0
Views: 118
Reputation: 36
use success() to get response of requested page
var responseText={};
$.ajax({url: 'controller.php',
type:"POST",
success: function(data)
{
responseText= data;
}});
Upvotes: 0
Reputation: 2228
You can use callback function after success
. Browser does not wait for a response and continue with code. so when browser get result, callback function will hit.
$.ajax({url: direc, success: doSmth});
function doSmth(data){
//do something with data
}
Upvotes: 0
Reputation: 618
Since ajax is async you have to use then:
promise.then( doneCallback, failCallback )
function testAjax() {
direc = "controller.php";
return $.ajax({
url: direc
});
}
var obj = testAjax();
obj.then(function(result) {
console.log(result);
}, function(xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
});
Upvotes: 1
Reputation: 712
This is asynchronous, you need to use the .done()
function. Take a look at the sample here.
Upvotes: 1