0

I have an Ajax script that is currently receiving a JSON data string and appending the data to a <li> on the page, like this:

enter image description here

This is the Jquery script I am using to do this:

jQuery(document).ready(function($) {
  $('form.quform').Quform({
  successStart: function (response) {
  var li = $("<li>").text(JSON.stringify(response));
  $("#response").append(li)

But I want to assign variables to the 5 field results and display them in spans so they look something like after I add css to them:

enter image description here

I have tried different things to get this to work, and read a lot of stack articles, One Stack article says to assign the string a variable like this var data = JSON.stringify(myObject, replacer);, but it has not worked. This is the last thing I have tried, but I know it is not right. Any help would be appreciated, thanks.

jQuery(document).ready(function($) {
  $('form.quform').Quform({
    successStart: function (response) {
      var data = JSON.stringify(myObject, replacer);
      var content = "balance: " + data.balance + " account_number:" + data.account_number;
      var li = $("<li><span>").text(content);
      $("#response").parent().append(li);

Also this is the JSON string I receive to the page:

{"type":"success","message":"Your message has been sent, thank you.","record":{"id":108,"bank_name":"Jane Doe","balance":"200","account_number":"8765432187654321","customer_id":"250","monthly":"50"}}
Community
  • 1
  • 1
Michael Falciglia
  • 1,046
  • 4
  • 20
  • 36

2 Answers2

0

Putting var data = JSON.stringify(myObject, replacer); there doesn't make sense because myObject and replacer are not defined. Don't just copy and paste code. The other answers just provided the general signature of JSON.stringify. You already know how to use it, because you used it in your first code snippet.


Anyways, JSON.stringify converts an existing object/array to a string containing JSON, which also means that response already is an object. So all you have to do is access its properties. In your case,

var data = response.record;

will set data to the correct value.

More info: Access / process (nested) objects, arrays or JSON

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Thanks for your help.... that makes sense, then how do I separate the fields names from the values when setting the variable. I don't want to show it like this `bank_name":"Jane Doe`, I would only want to show `jane doe` – Michael Falciglia Jan 19 '14 at 18:51
  • You don't have to. If `var x = {foo: "bar"};` is an object, then `x.foo` returns `"bar"`. Hence `data.bank_name` returns `"Jane Doe"`. This [MDN article](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects) explains quite well how objects work. – Felix Kling Jan 19 '14 at 18:53
  • I still am not getting it, am I suppose to change this also `var li = $("
  • ").text(JSON.stringify(response));` or just add this `var data = response.record;` on the next line. Sorry, I just have no experience with JSON.
  • – Michael Falciglia Jan 19 '14 at 19:28
  • In your second code snippet, replace `var data = JSON.stringify(myObject, replacer);` with `var data = response.record;`. You should also change `var li = $("
  • ").text(content);` to `var li = $("
  • ").text(content);`. `$("
  • ")` won't work.
  • – Felix Kling Jan 19 '14 at 20:03
  • oh don't use JSON you are saying – Michael Falciglia Jan 19 '14 at 20:05
  • Yes in the way you think about it, no in the general case. JSON is a language-indpendent, textual data exchange format (like XML, CSV or YAML). The server sends JSON as response to the client and then it can be *parsed* into JavaScript objects/arrays. In this case jQuery does the the parsing for you, hence `response` is already a JavaScript object and not a string containing JSON. `JSON.stringify` coverts objects/arrays *to* JSON. – Felix Kling Jan 19 '14 at 20:08
  • well yesterday someone suggested I use JSON after I posted a similar question and that is why I tried it. This is the thread I am talking about http://stackoverflow.com/questions/21209668/displaying-data-from-an-ajax-response-with-jquery – Michael Falciglia Jan 19 '14 at 20:18
  • FWIWI, JSON doesn't have to do anything with jQuery and whether you use JSON or not doesn't matter anymore at this point. All that's important is that `response` is an object. Looks like you were already on the right track with your code in the other question. – Felix Kling Jan 19 '14 at 20:39
  • I have been playing with it using this ` jQuery(document).ready(function($) { $('form.quform').Quform({ successStart: function (response) { var data = response.record; var content = "balance: " + data.record.balance + " account_number:" + data.record.account_number; var li = $("
  • ").text(content); $(".myData").parent().append(li);`
  • – Michael Falciglia Jan 19 '14 at 21:51