I'm using the $.post() function from jQuery to make a Ajax call with a JSON string. The call looks like this:

$.post(
    urlVar,
    jsonVar,
    function(data){
        //do stuff
    },
    'json'
)
.complete(function(){
    //do other stuff
});

To create jsonVar I'm using this code

var1 = {};
var1.id = fooId;
var1.amount = fooAmount;
var1.zoom = fooZoom;
jsonVar = JSON.stringify(var1);

To make the call work, jsonVar should look like this

{id:fooId, amount:fooAmount, zoom:fooZoom}

but it looks like this

{"id":fooId, "amount":fooAmount, "zoom":fooZoom}

Now my code will not work, because of the double quotes. I couldn't figure out how to get rid of those. Can anyone help me out?

IMPORTANT:

the code does work if I put the $.post() function like this:

$.post(
    urlVar,
    {id: fooId, amount: fooAmount, zoom: fooZoom},
    function(data){
        //do stuff
    },
    'json'
)
.complete(function(){
    //do other stuff
});
share|improve this question
4  
I'd interject that there might be something fundamentally wrong with the receiving end of the AJAX call, because that second JSON string is legal JSON syntax. In fact, I think that's actually standard (over the no-quotation-mark version). – Richard Neil Ilagan Jun 27 at 12:11
You example appears to be flawed in that "zoom" is never a part of var1 but is actually a part of var2. – Mark Schultheiss Jun 27 at 12:15
i had the same problem in different browsers. it worked after qouting the values in the json-string. like: {"id":"fooId", "amount":"fooAmount", "zoom":"fooZoom"} – some_coder Jun 27 at 12:15
@MarkSchultheiss My bad, that was a typo. – LuudJacobs Jun 27 at 12:17
1  
@LuudJacobs ~ if that works, then passing var1 directly into the AJAX call should work as well. Have you given that a try? – Richard Neil Ilagan Jun 27 at 12:35
show 5 more comments
feedback

3 Answers

Given this code:

var fooId = 'foodyfood';
var fooAmount = 10.20;
var fooZoom = 'zoomer';
var var2 = {};

var var1 = {}; 
var1.id = fooId; 
var1.amount = fooAmount; 
var2.zoom = fooZoom; 
jsonVar = JSON.stringify(var1); 
$('#showme').text(jsonVar);

the value shown in the showme would be:

{"id":"foodyfood","amount":10.2}

so your example appears to be flawed and actually is not JSON standard which specifies the double quotes.

EDIT: now your use of the post is the equivelent of:

$.ajax({
  type: 'POST',
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

except that it handles the object as objects in your second example after your edit.

see the example in the documentation here: http://api.jquery.com/jQuery.post/

with this form:

$.post("test.php", { name: "John", time: "2pm" },   function(data) {
     alert("Data Loaded: " + data);   
});

so if you have:

var mytest =  { name: "John", time: "2pm" };

it becomes: on the stringify

{"name":"John","time":"2pm"}

see working example here:http://jsfiddle.net/v4NHv/

share|improve this answer
one note, if you need a parameter to pass you might use $.param(var1); form which creates: id=foodyfood&amount=10.2 – Mark Schultheiss Jun 27 at 12:47
feedback

Change the post call like this

$.post(
    urlVar,
    {var1:jsonVar},
    function(data){
      //do stuff
   },
   'json'
)
.complete(function(){
   //do other stuff
});

And then of course you will need to make a little tweek to your receiving app

share|improve this answer
feedback

The JSON specification states that the keys must have double-quotes.

What do you mean your code won't work because of double quotes? Parse the JSON back into an object using JSON.parse; which is built-in to many modern browsers or you can shim it using the json2 library.

share|improve this answer
Regarding the parsing: $.parseJOSN() works as well – Johan Jun 27 at 12:26
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.