0

I send a post to the server to create a new table entry in my database. Once the table entry is created I have the server respond with the id of the table entry. In the chrome developer tools I can see the response as just a singular number (i.e. if its the fifth entry in the table the server response is just 5). How do I store this information using javascript/YUI to be used later? Do I have to do something with the Y.io on: success function?

EDIT:

    Y.io('/sessionsimulator/sessioncreate/', {
        method: 'POST',
        data: jdtoldstring,
        headers: {
          'Content-Type': 'application/json'
        },
        on: {
            success: buildtable()
        }           
    });

this is the code that posts the date/time and creates a session id. I can view the sqlite table afterwards and see that the session is created exactly how I wanted. the success function buildtable is the code that is called to generate the simulated data. within buildtable() i have a global variable that I am trying to set called sess_is

sess_id = Y.JSON.parse.responseText;

that statement lies within buildtable(), but when the table is created, the column that is filled with sess_id the variable is "undefined."

I can see in the developer tools the response to the url call /createsession is a number, I am just trying to pick that number and store it in sess_id variable.

Khamey
  • 481
  • 4
  • 17
  • what do you mean by later? in the same user/session or save it in the database? – Sergio Jul 05 '13 at 13:02
  • Please provide more information, like how long you want to store the data, if it ever needs to be removed, where exactly do you want it, etc. You could explore storing your data in cookies, html5 local storage, in the DOM, etc. – Jon Jul 05 '13 at 13:06
  • later as in one function later. the id will be used to generate simulated data. on success I want to call a function that creates simulated data and on one of the columns I have information for a session id. the session id should be populated with the server response. the process overall is hit button, post start time to database, return session id that was created for that start time, use this id along with other funcitons to create simulated data and create an html table. – Khamey Jul 05 '13 at 13:11
  • @Khamey post your code that makes "have the server respond with the id". Somewere you should give a variable to that response, depending a bit on how your code looks like – Sergio Jul 05 '13 at 13:12
  • edit made to provide more information – Khamey Jul 05 '13 at 13:43

1 Answers1

1

If the response is just a number, you can access it from response.responseText in your IO success callback. It's a string, so you need to parse it as a number:

Y.io(url, {
  //...
  on: {
    success: function (requestId, response) {
      var id = parseInt(response.responseText, 10);
      // do something with the id
    }
  }
});

It's usually a good idea to send JSON from the server and parse it in JavaScript when you want to send more information than just a number. You can read more about this in the IO User Guide, starting from the Response Object section.

juandopazo
  • 6,283
  • 2
  • 26
  • 29
  • Thank you, this worked perfectly. can you explain to me what the , 10 is doing though? I am unclear as to why you need it. – Khamey Jul 05 '13 at 13:47
  • The `10` is in order to specify that you're parsing a number in base 10 representation. If you don't include that, you may run into hard to find bugs down the road. You can read more about `parseInt` at [the Mozilla Developer Network wiki](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt). – juandopazo Jul 05 '13 at 21:50