Anyone able to help with doing a custom POST from editor, PLEASE

Anyone able to help with doing a custom POST from editor, PLEASE

danieljamesbertranddanieljamesbertrand Posts: 8Questions: 0Answers: 0
edited June 2012 in General
Hi,

I need someone to explain to me how to prevent editor from submitting an ajax request to a URL with it's own variables (I don't want action=edit in the PUT)

I only want my own d={"a" : 1, "b" : 2, "c" : "cow"}

something like this but I am not sure....

editor = new $.fn.dataTable.Editor( {
"ajaxUrl": "",
"domTable": "#usrTbl", <--- my data is in HTML table "usrTbl"
"fields": myFields, <--- myFields were set up for editor
"events" : {
"onPreSubmit": function (oData) {
var MYJSON = JSON.stringify(oData);
$.ajax({
"dataType": 'json',
"type": "PUT" ,
"url": baseURL + getAccountString + "?d={MYJSON}";
});
}
}
} );


Thanks in advance.

Replies

  • allanallan Posts: 63,180Questions: 1Answers: 10,411 Site admin
    As I mention in my PM, you would use onPreSubmit to manipulate the data being set to the server:

    [code]
    $(document).ready(function() {
    var editor = new $.fn.dataTable.Editor( {
    "ajaxUrl": "php/browsers.php",
    ...
    } );

    editor.on('onPreSubmit', function ( e, data ) {
    delete data.action;
    data.d = JSON.stringify( data );
    } );
    } );
    [/code]

    Allan
  • TimCoulterTimCoulter Posts: 1Questions: 0Answers: 0
    @allan: This solution does indeed do what the OP asked for but I wonder if it is possible to go a step further and replace the entire HTTP request body with a JSON string that represents the data object. I am expecting to receive something like this ...

    {"action": "create", "id": null, "table": null, "data": {"name": "My Name", "value": "My Value"}}

    I tried the following in the onPreSubmit event ...

    data = JSON.stringify( data );

    ... but it seems that the data object is subsequently re-evaluated when creating the HTTP request body. Is there anyway to bypass or disable this?

    Thanks.
  • allanallan Posts: 63,180Questions: 1Answers: 10,411 Site admin
    Hi Tim,

    Looking at the jQuery Ajax documentation it says:

    [quote]
    processData:
    By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded". If you want to send a DOMDocument, or other non-processed data, set this option to false.
    [/quote]

    ( http://api.jquery.com/jQuery.ajax/ )

    So I think this is what you are looking for.

    The $.ajax call that Editor makes can be overridden with Editor's 'ajax' initialisation option: http://editor.datatables.net/options/#ajax .

    Regards,
    Allan
This discussion has been closed.