DataTables with WebMethod - Page 2

DataTables with WebMethod

2»

Answers

  • jonrjonr Posts: 51Questions: 5Answers: 0

    I am going to set up a new page today with just the bare minimum requirements, I will also try out an ASMX for my method call BIndrid.

    Thanks for the suggestion Kevin, that is indeed what my code does now and its working fine. My ajax call returns the results and I push that straight into DataTables.

    I am just trying to get this working the way that it is supposed to.

    I will report back later.

    thanks all,

    jON

  • jonrjonr Posts: 51Questions: 5Answers: 0

    I have built a page from scratch with calls into an ASMX method.

    I have traced into the JQuery ajax call.

    It looks like my data proiperty is being mangled in the DataTables call whereas in my direct call the data argument (Criteria) is correctly shown as a json string.

    please see snap1 and snap2.

    if I change my data property sent to DataTables from:

                data: JSON.stringify({ 'Criteria': Criteria })
    

    to

            data: Criteria
    

    then the data property contains the string as seen in snap3

    if I then change the data property to this:

    data: '{"Criteria":{"Rno":"A-0598983-233"}}'
    

    I get the same mangled data property in the JQuery ajax call.

    jON

  • allanallan Posts: 63,121Questions: 1Answers: 10,397 Site admin

    AHH! Well damn - I should probably have thought of that. Using ajax.data as a string value is going to cause problems since it means that DataTables can't add any of its own properties to it (in this case it is JSON, but it could easily be some other format).

    Try this:

    ajax: {
      url: ...,
      data: function ( d ) {
        d.Criteria = Criteria;
        return JSON.stringify( d );
      },
      ...
    }
    

    I believe that will finally resolve the mystery.

    Thanks,
    Allan

  • jonrjonr Posts: 51Questions: 5Answers: 0

    Indeed it does Allan,

    many thanks,

    jON

  • jonrjonr Posts: 51Questions: 5Answers: 0

    Hi Bindred, probably slightly off topic but, contrary to my previous comment, an ASMX is probably a good place to store all my webmethods however, I am getting a 405 with a POST.

    As far as I can see this must be enabled in web.config to work.

    is this the only way that I can enable POST against these methods? I would rather not enable POST site wide so will probably revert to using POST to ASPX webmethods.

    If I can get this working then I can get my DataTable calls all in one module.

    thanks,

    jON

  • allanallan Posts: 63,121Questions: 1Answers: 10,397 Site admin

    Phew - got there in the end! I'll add information about this into the documentation and might actually put an error handler into DataTables core for it.

    Allan

  • jonrjonr Posts: 51Questions: 5Answers: 0

    I have hit another snag but the code in DataTables js is obsfucated, did you say that a clear version is available Allan?

    I am in the middle of a goobledygook bit of obsfucated code and can't glean enough in english to get an idea where the problem lies.

    jON

  • kthorngrenkthorngren Posts: 21,129Questions: 26Answers: 4,916
    Answer ✓

    You can remove the .min from the URL to get the full version. You can find the links here:
    https://datatables.net/download/release

    Kevin

  • allanallan Posts: 63,121Questions: 1Answers: 10,397 Site admin

    Yes, the obfuscated DataTables code is probably just the minified version. If you are using the files from the CDN you can just drop the .min part from the URL. That will work with everything apart from Editor. Editor is obfuscated and time limited for the trial. The licensed version includes the source for Editor.

    What is the error and the callback stack?

    Allan

  • jonrjonr Posts: 51Questions: 5Answers: 0

    It looks weird to me.

    We have 20 odd rows returned in the data, all looks good.

    in function _fninitialise in datatables.js

            // If there is default sorting required - let's do it. The sort function
            // will do the drawing for us. Otherwise we draw the table regardless of the
            // Ajax source - this allows the table to look initialised for Ajax sourcing
            // data (show 'loading' message possibly)
            _fnReDraw( settings );
        
            // Server-side processing init complete is done by _fnAjaxUpdateDraw
            var dataSrc = _fnDataSource( settings );
            if ( dataSrc != 'ssp' || deferLoading ) {
                // if there is an ajax source load the data
                if ( dataSrc == 'ajax' ) {
                    _fnBuildAjax( settings, [], function(json) {
                        var aData = _fnAjaxDataSrc( settings, json );
        
    here aData is undefined
    
                        // Got the data - add it to the table
                        for ( i=0 ; i<aData.length ; i++ ) {
                            _fnAddData( settings, aData[i] );
    
    

    so I get "cannot read property length of undefined" in the for next loop

    jON

  • jonrjonr Posts: 51Questions: 5Answers: 0

    Investigating further Allan, this code:

            // Compatibility with 1.9-. In order to read from aaData, check if the
            // default has been changed, if not, check for aaData         if ( dataSrc === 'data' ) {
                return json.aaData || json[dataSrc];
            }
    

    The object json has a property called "d" which contains the return rows of data.

    As such, the return line changed to

    json.aaData || json[dataSrc] || json["d"]
    

    works for me, I am not sure whether that is of help without delving into the code deeply.

    jON

  • allanallan Posts: 63,121Questions: 1Answers: 10,397 Site admin

    Use the ajax.dataSrc property to tell DataTables where to get your data from if it isn't in the default location of data.

    Allan

  • jonrjonr Posts: 51Questions: 5Answers: 0

    I didn't think that I decided where it was to go.Allan,

    How did I manage to tell it to use "d" in the first place?

    I'd rather fix that if I can but I don't have anything vaguely like an object called "d".

    I did think that it was perhaps because we sent in an object called "d" in your fix a few posts up here but changing the "d" to "data" didn't make any difference.

    thanks

    jON

  • allanallan Posts: 63,121Questions: 1Answers: 10,397 Site admin

    I'd rather fix that if I can but I don't have anything vaguely like an object called "d".

    I've never understood why .NET likes to wrap things in a d object for JSON - it doesn't make any sense to me. You'd need to look into the .NET world and the JSON response stuff. I think that if you return JSON yourself then it will display things correctly - but if you let .NET does its magic, it gets wrapped.

    Allan

This discussion has been closed.