views:

38

answers:

1

I have following ajax call:

  var empId = $(this).attr('name').replace(/disp/,'');
   $.ajax({
      url: '<%= Url.Action( "AjaxDisp" ) %>',
      data: { id: empId, format: 'json' },
      dataType: 'json',
      success: function(data,status) {
         // populate the popup and display it            
      }
   });

empId is there(I use alert(empId); to test it, it it okay). But in action method AjaxDisp(int id, strng format), I can get the id, but I can get format = "json".

Why?

+1  A: 

Convert the object (passed to data) to JSON string.

 var empId = $(this).attr('name').replace(/disp/,'');
   $.ajax({
      url: '<%= Url.Action( "AjaxDisp" ) %>',
      data: JSON.stringify({ id: empId, format: 'json' }), //converting your object to JSON string.
      dataType: 'json',
      success: function(data,status) {
         // populate the popup and display it            
      }
   });
SolutionYogi
Thanks. When try it and run it. It said JSON is not defined.
KentZhou
You will have to include this JSON library http://www.json.org/json2.js
SolutionYogi
The native JSON parser is available in Internet Explorer 8 and FireFox 3.1+ For other browsers, you'll need the JSON library.
Bryan Migliorisi