1

I have an array Like

info['mk'] = 'hi';
info['pk'] = 'hello';
info['wk'] = 'hi';
info['rk'] = 'hello';

And i want to convert in json and send through ajax .

5
  • 5
    what have you ever tried? Commented Dec 4, 2014 at 6:54
  • i have tried var arraywhole = JSON.stringify(info); Commented Dec 4, 2014 at 6:55
  • stackoverflow.com/questions/19970301/… Commented Dec 4, 2014 at 6:58
  • And as a reminder, JSON.stringify() does not work on all browsers. See this Commented Dec 4, 2014 at 7:06
  • Ok i`ll keep this in mind. Commented Dec 4, 2014 at 7:08

4 Answers 4

1
info = {}; //must be set 
info['mk'] = 'hi';
info['pk'] = 'hello';
info['wk'] = 'hi';
info['rk'] = 'hello';

then JSON.stringify(info);

Sign up to request clarification or add additional context in comments.

2 Comments

You should explain JSON.stringify() only works on Object, not Array.
Yes Raptor,Now i got it that JSON.stringify() only works on Object, not Array, thank you for helping me.
1

Try this:

info={}
info['mk'] = 'hi';
info['pk'] = 'hello';
info['wk'] = 'hi';
info['rk'] = 'hello';

$.ajax({
  type: "POST",
  dataType: "json",
  data: JSON.stringify({info:info}),
  url: "",
  success: function(msg){

}

});

Comments

0

You should try something like this:

$.ajax({
        type: "post",
        url: "target",
        data: info
    });

Comments

0

You could try like this

var info = [];
var tmpObj = {};
info['mk'] = 'hi';
info['pk'] = 'hello';
info['wk'] = 'hi';
info['rk'] = 'hello';
tmpObj.arr = info;
$.ajax({
    type: "post",
    url: "target",
    datatype:"json",
    data: tmpObj
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.