0

I'm getting error in the method $.toJSON(batch) that object does not support this property or method. How can i pass batch array using $.toJASON(batch) method?

$.ajax({
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            url: '@Url.Action("BatchUpdate", "Home")',
            dataType: 'json',
            //data: '{viewModelsBatch: '+batch+'}',
            data: $.toJSON(batch),
            success: function(result) {
              //...
              $('#jqgProducts').trigger('reloadGrid');
            }
    });
Justin Ethier
  • 131,333
  • 52
  • 229
  • 284
Siddiqui
  • 172
  • 1
  • 3
  • 16

2 Answers2

1

I agree with Paul Grime that the usage of JSON.stringify from json2.js is better as toJSON jQuery plugin. The reason is that JSON.stringify is native implemented in the most web browsers. You should include json2.js only to be sure that if the web browser do have no support of JSON.stringify the implementation in JavaScript will be used. The typical jQuery plugin is on the other side pure JavaScript code. So the serialization will be slowly. Moreover the author of json2.js is Douglas Crockford - the author of JSON standard and very famous specialist in JavaScript language. So his implementation of JSON serialization in JavaScript is really the best.

Your main question was another: why data: $.toJSON(batch) or data: JSON.stringify(batch) will not work in your code. The problem is not jqGrid problem, but pure server side problem. You don't wrote what technology you use on the server side. The code looks like ASP.NET MVC code, but it's unclear which version of ASP.NET MVC you use. If you use version 3.0 it should work automatically, but in case of 2.0 version you need register custom JsonValueProviderFactory (see here).

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • I'm using version 3.0.0.0 of mvc, i've included json2.js file but still getting same error. :( – Siddiqui Sep 26 '11 at 04:41
  • @farhan: In any way it's pure ASP.MVC problem. So you should include at least the prototype of `BatchUpdate` action which you use and the test `batch` which can be used to reproduce your problem. – Oleg Sep 26 '11 at 08:17
0

I don't know what the toJSON function is, so I'd recommend reading this answer.

Serializing to JSON in jQuery

Community
  • 1
  • 1
Paul Grime
  • 14,970
  • 4
  • 36
  • 58