Using $.ajax with RESTful ajaxUrl()

Using $.ajax with RESTful ajaxUrl()

burncharburnchar Posts: 118Questions: 12Answers: 0
edited August 2012 in Editor
I need to have the Editor send JSON and only JSON to the server when doing a Create or Edit operation.
Specifically, my MVC4 controller gets data containing "action", "table", "id", and "data" -- Editor's modus operandi.

Because "data" differs (different column names/values for different tables), it needs to be a generic data type. Microsoft's urlencoded deserializer can't handle that, but I believe it's JSON parser can.

I looked at: http://datatables.net/forums/discussion/10877/mvc3-using-datatable-and-editor and don't like that approach (to making Editor work with MVC) because I'd have to duplicate several fields for every table (DRY principle).

Then I saw you mention $.ajax here: http://datatables.net/forums/discussion/10733/how-to-overcome-to-the-cross-domain-limitation-in-http-using-editor/p1

The problem (I think) is that ajaxUrl() and $.ajax() conflict with regards to using POST/PUT/DELETE etc. In my code below, $.ajax() says "POST" but when I edit a field, ajaxUrl() says PUT.
I can't simply omit the field in $.ajax() though.

[code]
"ajaxUrl": {
"create": "POST /API/Species",
"edit" : "PUT /API/Species/_id_",
"remove": "DELETE /API/Species"
},

"ajax": function ( url, data, successCallback, errorCallback ) {
$.ajax( {
"type": "POST", //!!!!!!!!!!
"url": url,
"data": data,
"dataType": "json",
"success": function (json) {
successCallback( json );
},
"error": function (xhr, error, thrown) {
errorCallback( xhr, error, thrown );
}
} );
},
[/code]

This gives me "Microsoft JScript runtime error: DOM Exception: HIERARCHY_REQUEST_ERR (3)" running JQuery's append function (this is a development system using Visual Studio). It gives me the same error when I change $.ajax() to use "PUT" (when editing a field).

The question is: How to I make Editor sent AJAX and only AJAX (not "application/x-www-form-urlencoded; charset=UTF-8") and work with the RESTful fetures of Editor 1.1?

Replies

  • burncharburnchar Posts: 118Questions: 12Answers: 0
    I may be misunderstanding the "url" parameter of $.ajax(), but my question would still apply.
  • allanallan Posts: 51,411Questions: 1Answers: 7,752 Site admin
    > "ajax": function ( url, data, successCallback, errorCallback ) {

    That's an Editor 1.0 prototype for that function - there was actually a small change to it for 1.1 that took into account the fact that the method can easily be changed with ajaxUrl - it should now be:

    [code]
    "ajax": function ( method, url, data, successCallback, errorCallback ) {
    [/code]

    (the example in the documentation is actually wrong there, sorry about that - the parameter list is correct, the example isn't... that will be fixed shortly!).

    This way you can just give the 'method' parameter of the object the 'method' parameter of the function :-).

    Is that the only change needed? You mention:

    > I need to have the Editor send JSON and only JSON to the server

    This doesn't really do that. Do you mean JSON in string representation? For example:

    [code]
    d = '{"myParam": "myValue", ... }';
    [/code]

    and then the server can decode what 'd' is? For that, you would just need to stringify the 'data' object passed into the ajax method, and have that being sent to the server.

    Regards,
    Allan
  • burncharburnchar Posts: 118Questions: 12Answers: 0
    Allan,

    Getting that simple solution from you -- the use of $.ajax, "method", and finding JSON.Stringify, has a multi-layered story months in the making involving betas of Microsoft products, financial transactions, and digging into library source code. If only you knew the joy of actually solving this problem.

    Actually, I'm sure you've run into more than a few of those developing DataTables.

    The alternative would have been making dozens of near duplicate objects in my code, and I just psychologically cannot do that. It would be maddeningly inelegant.

    Thank. You. Allan.
  • allanallan Posts: 51,411Questions: 1Answers: 7,752 Site admin
    You are very welcome indeed! I'm delighted I could help :-).

    Regards,
    Allan
This discussion has been closed.