Andrus Andrus - 2 months ago 5
Ajax Question

how to pass json in query string parameter with jquery $.get

Jquery $.get should send single query string parameter _filters whose value is json string:

API/MyGet?_filters='{groupOp:"AND",rules:[{field:"Toode",op:"eq",data:"xxxx"}]}'&sort=code


I tried

$.get( 'API/MyGet', {
_filters:{ groupOp:"AND",
rules:[{field:"Toode", op:"eq", data:'xxxx'}]
},
sort: 'code'
},
function(data) {
alert( JSON.stringify(data) );
}
);


but Chrome developer tools show that this creates incorrect and strange query string:

_filters[groupOp]=AND&_filters[rules][0][field]=Toode&_filters[rules][0][op]=eq&_filters[rules][0][data]=


How to force jquery get to pass proper query string ?

Answer
var params = {
  sort: '...',
  _filters: JSON.stringify({
    'key': 'value'
  })
};

$.get('API/MyGet', params, function(data) {
  alert( JSON.stringify(data) );
});
Comments