How do I trap a return of 403 in the new version

How do I trap a return of 403 in the new version

wjhumphreyswjhumphreys Posts: 52Questions: 9Answers: 5
edited July 2014 in DataTables 1.10

I'm getting a lot of conflicting information on how to trap return data from an Ajax call.

This is MS MVC based but Im returning


context.HttpContext.Response.StatusCode = 403; context.Result = new JsonResult { Data = new { Error = "NotAuthorized", LogOnUrl = urlHelper.Action("LogOn", "Account") }, JsonRequestBehavior = JsonRequestBehavior.AllowGet };

if a session has expired.

Ive tried using


"dataSrc": function (json) { alert(json.data); return json.data; }

but the json returned is blank and I cannot check for the 403 unless Im missing something.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin

    At the moment you would need to provide ajax as a function and define your own $.ajax() call with whatever error handling you need.

    The ajax.dataSrc option is designed simply to transform the data retrieved from the server from one form into another, and has no access to the underlying XHR object.

    Extending DataTables abilities here is certainly something I want to look at - thanks for flagging it up.

    Allan

  • wjhumphreyswjhumphreys Posts: 52Questions: 9Answers: 5

    Could you give me a simple example of this as I cant quite work out what is to go where.

    I'm currently using


    "url": "/FormAjaxHandler/Incomplete", "type": "POST", "data": function (d) { d.foo1Id = $("#Foo1Id").val(); d.foo2Id = $("#Foo2Id").val(); },

    How does that translate to this?


    "ajax": function (data, callback, settings) { callback( JSON.parse( localStorage.getItem('dataTablesData') ) ); }
  • allanallan Posts: 63,368Questions: 1Answers: 10,449 Site admin
    Answer ✓

    The documentation probably needs an example of this I guess. You would do something like:

    ajax: function ( data, callback, settings ) {
      $.ajax( {
        url: ...,
        data: data,
        dataType: 'json',
        success: function ( json ) {
          callback( json );
        }
      } );
    }
    

    Obviously you will fill out the $.ajax() call as you need using standard jQuery options.

    Allan

  • wjhumphreyswjhumphreys Posts: 52Questions: 9Answers: 5

    Ahhh that makes more sense. I definitely think this is something you need to update in the documentation as this alone solves GETs that are to long, removing data from posts that isn't required. I have no doubt this is really something to people like myself that like full control and hand everything over to the server-side of things but without it some things are a bit of a pain.

    :-)

This discussion has been closed.