We help IT Professionals succeed at work.
Get Started

Loading a JSON array into a variable from an AJAX response

delux220
delux220 asked
on
1,378 Views
Last Modified: 2012-05-05
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!
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();
  }
});

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Expert of the Year 2008
Top Expert 2008
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE