delux220
asked on
Loading a JSON array into a variable from an AJAX response
Hi all. So I'm trying to implement a MVC pattern for a Contact Import function. The page allows the user to log into his gmail account, which makes an ajax call to the serverside to get all the user's contacts from his address book. The contacts are made into a JSON Array in string form and then returned in the response to the client-side.
I want to take the JSON array in the response and store it in a View, so that I can play with the JSON array in later steps. Is there a way I can do this in a clean way?
Ideally, I'd like to set the instance variable to the json array in the Ajax.Request's onComplete. However, nothing I have tried seemed to have work. It looks like there is an issue with scope.
What I have tried that does work is what is below. I have a load function inside the view that makes the ajax call. The request is saved as an instance variable and receive() is called to extract the response from the instance variable request. I used prototype responders to call the receive function after the ajax call is complete. While this works, I am still a noob with anything close to pattern-driven ajax, and I need opinions on how to make this less spaghetti code.. I am offering 500 points out of desparation!
I want to take the JSON array in the response and store it in a View, so that I can play with the JSON array in later steps. Is there a way I can do this in a clean way?
Ideally, I'd like to set the instance variable to the json array in the Ajax.Request's onComplete. However, nothing I have tried seemed to have work. It looks like there is an issue with scope.
What I have tried that does work is what is below. I have a load function inside the view that makes the ajax call. The request is saved as an instance variable and receive() is called to extract the response from the instance variable request. I used prototype responders to call the receive function after the ajax call is complete. While this works, I am still a noob with anything close to pattern-driven ajax, and I need opinions on how to make this less spaghetti code.. I am offering 500 points out of desparation!
function ContactView() { }
ContactView.prototype = {
contact_queue : '',
template : null,
request : null,
initialize : function() {
},
load : function(form) {
var params = form.serialize();
this.req = new Ajax.Request('/wom_app/RegistrationServlet',
{
method:'post',
parameters: params,
onComplete: function(transport){
//Could I put code here to call //receive?
},
onFailure: function(){
alert('Something went wrong...');
}
});
},
receive : function() {
this.contact_queue = req.transport.responseText;
alert(this.contact_queue);
}
}
var c = new ContactView();
Ajax.Responders.register({
onCreate: function() {
alert("create");
},
onComplete: function() {
c.receive();
}
});
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Why the "B" grade?