1

OK for starters this is a mobile app that i am building with PhoneGap. I am sending request over Cross-domains. using JSONP and ajax and sending response from Rails 3 with JSON.

I want to receive the data and parse it out into separate records so i can display it in a table.

i have tried

JSON.parse(data)

JSON.parse(data.results)

when i alert:

alert(data)
====> [object OBJECT]

alert(data.results)
====> [object OBJECT],[object OBJECT],[object OBJECT]

and have been unsuccessful

AJAX call:

    $.ajax({

      type: "GET",

      dataType: "jsonp",
      // test
      url: "http://192.168.1.65:3000/" + ajax_url,

      cache: false,

      //data: ajax_data,
      data: 'auth_token='+ auth_token + '&' + ajax_data,
      success: function(data) {
          if (data.response.status === 200 && callback.onSuccess) {
              callback.onSuccess.call(this, data.response.data);
          }
          else if (data.response.status >= 400 && callback.onError) {
              callback.onError.call(this, data.response.data.error);
          }
          if (callback.onComplete) {
              callback.onComplete.call(this, data.response.data);
          }
      }
    });

Rails response code:

render :json => { :response => { :status => 200, :data => {:results => results} } }, :callback => params[:callback]

I am receiving the response and when i console log the data i get this:

enter image description here

When i console log data.results i get this

enter image description here

Edit to Question:

i dont want to hard code data.results[0].variable for each variable then data.results[1].variable there could be 100's

Big Al Ruby Newbie
  • 834
  • 1
  • 10
  • 30
  • "have been unsuccessful" ok... anything else you can tell us? Why don't you just use the `data.results` data as is since it obviously is what it appears to be? – sebnukem Dec 24 '14 at 17:13
  • 1
    In a JSONP request, you don't have to parse the result. Under the hood it is a mere JS function call with an object and no JSON. – Sirko Dec 24 '14 at 17:13
  • `$.ajax` looks like the typical jQuery function call. jQuery decodes everything automatically. – Álvaro González Dec 24 '14 at 17:15
  • So how would i automate the results parsing... i dont want to hard code data.results[0].variable then data.results[1].variable there could be 100's – Big Al Ruby Newbie Dec 24 '14 at 17:16
  • you loop over the data array and use the index from the loop. For example use `$.each(data, function(index){ /* parse to html*/})` – charlietfl Dec 24 '14 at 17:32
  • I am sorry i am tring to learn all this. Could you perhaps put that into an answer with at least 1 of the variables parsed into HTML – Big Al Ruby Newbie Dec 24 '14 at 17:44
  • can you provide sample what you want to do? – Grundy Dec 24 '14 at 17:55
  • if you are concerned about hardcoding, you can iterate over the `json` object as `for(var result in data.results){console.log(data.results[result])}` – AdityaParab Dec 24 '14 at 17:59

0 Answers0