How to overcome to the Cross Domain limitation in HTTP using Editor?

How to overcome to the Cross Domain limitation in HTTP using Editor?

arieldistefanoarieldistefano Posts: 4Questions: 0Answers: 0
edited July 2012 in Editor
I've tried to create a proxy in PHP, but the Editor Ajax call give me the JSON data transformed into www-form-url-encoded, so I can just Re-post the data from the PHP.

Do you have any example Allan about this?

Replies

  • jcreadyjcready Posts: 44Questions: 0Answers: 0
    edited July 2012
    If you have access to the server you're trying to save data to, you could simply include a Access-Control-Allow-Origin header. So long as you're sending your request with a content-type of "application/x-www-form-urlencoded", "multipart/form-data", or "text/plain" then adding that header to the server's response should be all you need. If you know the domain of where you're going to be sending the data from (i.e. http://example.com/myDataTableEditor) then the server's Access-Control header should look like this:

    [code]Access-Control-Allow-Origin: http://example.com[/code]

    If, however, you don't know the domain (or you're developing on localhost) then the header would have to be set to:

    [code]Access-Control-Allow-Origin: *[/code]
  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin
    In addition to what jcready suggests, are you in control of the remote server as well? If so, you can use JSONP to cope with cross domain requests. This example shows how DataTables' server-side processing can be done with JSONP, but the same principle applies to Editor as well: http://datatables.net/release-datatables/examples/server_side/jsonp.html .

    In Editor you would use the 'ajax' option ( http://editor.datatables.net/options/#ajax ) to make an Ajax request with a JSONP data type - in the same way DataTables uses fnAjaxData (Editor simplifies a lot of the naming :-) ).

    Allan
  • arieldistefanoarieldistefano Posts: 4Questions: 0Answers: 0
    Thank you both!.

    We solved yesterday creating a proxy PHP in our side and here the code with all the params (after a couple of hours we learned that "processData": false is needed to receive the POST objects as it is in PHP .

    "ajax": function (method, url, data, successCallback, errorCallback) {
    $.ajax( {
    "type": "POST",
    "url" : "../datasource/execute_action.php",
    "data": JSON.stringify(data),
    "contentType" : "application/json",
    "dataType": 'json',
    "processData": false,
    "cache": false,
    "success": function (json) {
    //successCallback( json );
    },
    "error": function (xhr, error, thrown) {
    //errorCallback( xhr, error, thrown );
    }
    } );
This discussion has been closed.