23 June 2010

jQuery's ajax request by POST method cause the security validation error

To avoid security validation error:
- First you need to make sure that your web.config file is updated to support Ajax.Net. In turn that will enable you to use ajax calls with json parameters. There are a lot of changes there, so be careful.
- Use Fidler to see the actual request/response, this may shed some more light on the matter
- Make sure GetDataMethod is a static
- Make sure 'data' is a valid jason object passed as a string
- Make sure 'dataType' is 'json', assuming you updated the web.config
- Make sure 'contentType' is 'application/json; charset=utf-8'
Finally, the proper ajax POST format is like so:

$.ajax({
    'type'  : 'POST',
    'url'  :  'GetSampleData.aspx/GetDataMethod',
    'data' :  '{data:"abcdfg"}',
    'contentType': 'application/json;  charset=utf-8',
    'dataType'  : 'json',
    'success' :  function (data) {
    //process data
    },
    'error'  :  function (xhttp, textStatus, errorThrown) {
    //show error
    }
});