function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
vigoroushakevigoroushake 

Visualforce - Javacript - Datatables - Remote Action: Synchronous XMLHttpRequest Error

I'm tyring to develop Visualforce page that leverages datatables and ajax to render a table with data passed from a controller using @RemoteAction. The folllowing is the core code for the datatable:

        var OrderNumber = {!OrderNumber};
        var OrderNumberString = OrderNumber.toString();
                    
        j$ = jQuery.noConflict();
        j$(document).ready( function () {
                
            var invoiceTable = j$('[id$="invoicetable"]').DataTable({
                "language": {
                 "loadingRecords": "",
                 "emptyTable": "No data available"
                  },
                
                // Get Invoice Items associated with the Order in question.
                "ajax": function(data, callback, settings){ 
                     Order_BillingIntCtrlExt.getInvoiceItems(OrderNumberString,function(err,records){
                         if(err) alert(err.message);
                     else{
                         callback({'data': records});
                         }
                     }); 
                 },     
                    
                // Specify our columns. The first column is used to control expanding and collapsing.
                "columns": [
                    { "class": 'details-control',
                        "orderable": false,
                        "data": null,
                        "defaultContent": '',
                        width: "8%",
                        },                  
                        
                    {"data": "_props.Invoice_Date__c",
                        "defaultContent": '' },
                    {sClass: "alignRight", "data": "_props.formattedInvoiceMerchandiseAmount",
                        "defaultContent": '' },
                    {sClass: "alignRight", "data": "_props.formattedInvoiceHandlingAmount",
                        "defaultContent": '' },
                    {sClass: "alignRight", "data": "_props.formattedInvoiceFreightAmount",
                        "defaultContent": '' },
                    {sClass: "alignRight", "data": "_props.formattedInvoiceAddtionalChargeAmount",
                        "defaultContent": '' },
                    {sClass: "alignRight", "data": "_props.formattedInvoiceAddtionalFreightAmount",
                        "defaultContent": '' },
                    {sClass: "alignRight", "data": "_props.formattedInvoiceTaxAmount",
                        "defaultContent": '' },
                    {sClass: "alignRight", "data": "_props.formattedInvoiceGSTAmount",
                        "defaultContent": '' },    
                    {sClass: "alignRight", "data": "_props.formattedInvoicePSTAmount",
                        "defaultContent": '' },
                    //{sClass: "alignRight", "data": "_props.formattedInvoiceDiscountAmount",
                    //    "defaultContent": '' },                                    
                    {sClass: "alignRight", "data": "_props.formattedInvoiceTotal",
                        "defaultContent": '' },
                    //Hide the childData column. It is still searchable
                    {"data": "_props.childData",
                        "defaultContent": '', "visible": false },                                 
                    ],
                order: [[1, 'asc']],
            } );

do other stuff with table...

The following is my @RemoteAction method on my controller:

 @RemoteAction
  global static CW_Invoice__x[] getInvoiceItems(string OrderNumber){
    CW_Invoice__x[] cwInv =  CWConnection.getCWInvoiceAllFields(OrderNumber);
    System.debug('CW Invoices Size: ' + cwInv.size());
  return cwInv;  
  }

When I run this code the table renders with no data and I get the following error in my console:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.

I'm also receiving errors relating to the returned data being undefined... It appears as though the datatable is rendering without waiting for the returned data.

Any pointers much appreciated?



 
Andy BoettcherAndy Boettcher
Are you using apex:includeScript in place of normal HTML script tags?  I've seen that error if I don't use apex:includeScript.

Also all RemoteActions run asynchronously.  Don't use standard jQuery ajax calls - use the Visualforce.remoting.Manager.invokeAction method as shown here:  https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_js_remoting_example.htm