0

I want to get JSON data using POST method in Ajax. I used below code to retrieve the data but it gives an error like

TypeError: Illegal invocation

http://mylocalhost.com/statics?action=retrive&table=log

On above link I got data in JSON format. Here mylocalhost.com is replaced by my IP and port number.

var domain_url = "http://mylocalhost.com/statics";

var obj = {
    action: 'retrive',
    table: 'log',
    dataType: 'json',
    processData: false,
    limit: 20,
    option: {_id: {$lt :id}}
};

console.log(obj)

$.ajax({
    url: domain_url,
    type: 'POST',
    data: obj,
    success: function(data, res){
        console.log(data)
    },
    error: function (errorThrown, res)
    {
    }
});
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Gunjan Patel
  • 2,342
  • 4
  • 24
  • 45
  • 1
    Which line is the error happening on? – Barmar Oct 07 '14 at 14:09
  • 2
    I think it's probably having an issue with `option: {_id: {$lt :id}}`. I don't think you can pass a nested object in as part of the data object. Each value needs to be a string. – Andy Oct 07 '14 at 14:10
  • 1
    What is in the variable `id`? That is your problem. – epascarello Oct 07 '14 at 14:16
  • @Andy I just tested out a triple-nested object, `$.ajax({ url: "/", type: "POST", data: {a:{_id:{$lt: 5}}} })`, and it works okay; jQuery's resulting query string is `a[_id][$lt]=5`. I thought maybe it was a a circular-reference issue, but that produces "`maximum call stack size exceeded`". There doesn't seem to be enough here to reproduce the issue. – apsillers Oct 07 '14 at 14:18
  • To apply the duplicate to your problem: the value of `id` is either a DOM object or something that contains a DOM object (e.g., a jQuery object). – apsillers Oct 07 '14 at 14:31

1 Answers1

1

The error is occurring when jQuery is trying to take the object you pass in and convert it to parameters to pass to the server. The issue is with what ever is in your variable id. When it is a string/number it is fine.

var id = 123;
var opt = {option: {_id: {$lt : id}}};
var qs = $.param(opt);  //fine

The error normally comes when someone tries to reference an attribute of an object, but ends up just setting the object.

var id = document.getElementsByTagName("input")[0];
var opt = {option: {_id: {$lt : id}}};
var qs = $.param(opt);  //results in "Illegal invocation"

So the value store in id is probably not what you think it is.

epascarello
  • 204,599
  • 20
  • 195
  • 236