2

I return an array with my ajaxCall that looks like this: http://d.pr/i/ojR4

Inside the objects is Json (example: http://d.pr/i/aAM2) When i push this to my .hbs file (backbone) it's not possible to loop trough it, because it's an array, and it only accepts plain JSON.

Is there any way to convert this entirely to JSON?

My renderfunction in my view below:

render: function(){
    var self = this;
    var tripData;
    console.log("[TripListView] render");
    $.ajax({
        url: Util.api + "/getalltrips",
        type:"GET",
        success: function(data){
            console.log(data); // This is the output given
            tripData = data;

        }, error:function(){
            console.log(arguments);
        }
    });
    $('#container').html(this.template({trips: data}));
    return this;
}
7
  • 1
    There is no such thing as "plain JSON". Commented May 23, 2013 at 0:35
  • What actual problem are you having? Commented May 23, 2013 at 0:35
  • I have an array with objects in it. In the objects is JSON. I need to remove the array and make that JSON. Kinda hard to understand i know, ain't easy to explain ether. What i have now is array(object,object,object,object) and i need object(object,object,object) Commented May 23, 2013 at 0:39
  • 1
    Do you know that Ajax is asynchronous? Commented May 23, 2013 at 0:40
  • 1
    Your actual problem is that the callback didn't run yet. AJAX is asynchronous. Commented May 23, 2013 at 0:41

2 Answers 2

1

If you can't send an array wrap it in an object and use it. But cause could be because of accesing data out side of AJAX call.

$('#container').html(this.template({trips: data}));<-- here data would be undefined

Try this:-

 var self = this
 success: function(data){
            console.log(data); // This is the output given
            tripData = {tripsResponse:data};// You probably don't need this since your actual issue might be accessing `data`  below the ajax call.
            $('#container').html(self.template({trips: tripData })); // I have moved this here since placing this after ajax call doesn't make sense, as it would have got executed before your callback.
        }
       //....
Sign up to request clarification or add additional context in comments.

2 Comments

Superb. Worked like a charm. Thank you very much! (and i had to put the $('#container).. in my successfunction indeed. Pretty stupid I didn't saw that. You fixed two of my problems when I was only asking about one. Thx a lot!
Yep, i know. I had to wait 5 more minutes and forgot about it then. ;) Done btw.
0

I'm not exactly sure what you're problem is but I'll take a guess. If you create a Dictionary<object, object> and set your key to some string like 'data' and the value to be your array, when you serialize it, you'll get a JSON object. Here's a short example:

var theList = new List<string>();
var theDict = new Dictionary<string, object>();
theDict.Add("data", theList);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.