Searchbuilder javascript errors when sending JSON [solved]

Searchbuilder javascript errors when sending JSON [solved]

AmirAMAmirAM Posts: 3Questions: 0Answers: 0

A better solution to the following discussions:
#76987
#76989

As the title and those threads explain the issue. Here is a solution that doesn't involve changing library source code:
set processData to false and change beforeSend as follow:

ajax: {
      url: `/url`,
      type: 'POST',
      contentType: "application/json",

      processData: false, // "tells jquery not to process or transform the data object before sending it to the server"
      
      data: function (d) {
        d.custom_filters = getFilters();
        return d; // do not JSON.stringify in here and return object
      },

      beforeSend: (xhr, settings) => {
            xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
            xhr.overrideMimeType('application/json');

            if (typeof settings.data !== 'string') {
                settings.data = JSON.stringify(settings.data);
            }
       },
},

Replies

  • allanallan Posts: 64,216Questions: 1Answers: 10,598 Site admin

    Thanks for posting this! I've committed a new feature for DataTables 2.3 which addresses the issue highlighted here. Instead of using ajax.data to return a JSON string, I've added a new ajax.submitAs option which can be set to json. This ensures that the original data object, remains an object all the way through the preXhr process.

    Planning to release 2.3 next month.

    Allan

Sign In or Register to comment.