Changes between Version 4 and Version 5 of 2017:notasDeDesarrollo


Ignore:
Timestamp:
May 5, 2017, 11:08:10 AM (7 years ago)
Author:
aaraujo
Comment:

Agregada sección con información sobre cookies en peticiones de dominios cruzados.

Legend:

Unmodified
Added
Removed
Modified
  • 2017:notasDeDesarrollo

    v4 v5  
    2424* [https://tibisay.cenditel.gob.ve/murachi/changeset/00fba4a420d767345dcc6517161e61029d59d3cf/murachi 00fba4a]
    2525
     26=== Cross-Domain Cookies ===
     27
     28[http://stackoverflow.com/questions/3342140/cross-domain-cookies Cross-Domain Cookies]
     29
     30
     31Para que las solicitudes de dominios cruzados funcionen es necesario habilitar CORS. En el caso de cookies se utiliza la política del mismo origen, en este sentido si se habilita CORS se pueden recibir y usar las cookies de un servidor B para establecer una sesión persistente desde el servidor A al servidor B.
     32
     33En el servidor B se debe configurar el CORS para que responda {{{Access-Control-Allow-Credentials: true}}} en el encabezado de respuesta.
     34
     35En el servidor A se debe utilizar la bandera {{{withCredentials}}} en todas las peticiones agregando {{{xhrFields: {withCredentials: true}}}}
    2636
    2737
    2838
     39{{{
     40$.ajax({
     41
     42  // The 'type' property sets the HTTP method.
     43  // A value of 'PUT' or 'DELETE' will trigger a preflight request.
     44  type: 'GET',
     45
     46  // The URL to make the request to.
     47  url: 'http://html5rocks-cors.s3-website-us-east-1.amazonaws.com/index.html',
     48
     49  // The 'contentType' property sets the 'Content-Type' header.
     50  // The JQuery default for this property is
     51  // 'application/x-www-form-urlencoded; charset=UTF-8', which does not trigger
     52  // a preflight. If you set this value to anything other than
     53  // application/x-www-form-urlencoded, multipart/form-data, or text/plain,
     54  // you will trigger a preflight request.
     55  contentType: 'text/plain',
     56
     57  xhrFields: {
     58    // The 'xhrFields' property sets additional fields on the XMLHttpRequest.
     59    // This can be used to set the 'withCredentials' property.
     60    // Set the value to 'true' if you'd like to pass cookies to the server.
     61    // If this is enabled, your server must respond with the header
     62    // 'Access-Control-Allow-Credentials: true'.
     63    withCredentials: false
     64  },
     65
     66  headers: {
     67    // Set any custom headers here.
     68    // If you set any non-simple headers, your server must include these
     69    // headers in the 'Access-Control-Allow-Headers' response header.
     70  },
     71
     72  success: function() {
     73    // Here's where you handle a successful response.
     74  },
     75
     76  error: function() {
     77    // Here's where you handle an error response.
     78    // Note that if the error was due to a CORS issue,
     79    // this function will still fire, but there won't be any additional
     80    // information about the error.
     81  }
     82});
     83}}}
     84Fuente: [https://www.html5rocks.com/en/tutorials/cors/]
     85
     86
     87En el caso de consumir recursos del servicio murachi con Ajax:
     88
     89{{{
     90$.ajax({
     91                //url: "https://192.168.12.154:8443/Murachi/0.1/archivos",               
     92                url: "https://murachi.cenditel.gob.ve/Murachi/0.1/archivos",
     93                type: "post",
     94                dataType: "json",
     95                data: formData,
     96                cache: false,
     97                contentType: false,               
     98                                processData: false,
     99                                // Set the value to 'true' if you'd like to pass cookies to the server.
     100                                // If this is enabled, your server must respond with the header               
     101                                // 'Access-Control-Allow-Credentials: true'.
     102                                xhrFields: {withCredentials: true},
     103                                headers: {"Authorization":"Basic YWRtaW46YWRtaW4="},
     104                                success: function(response) {
     105                                                alert(JSON.stringify(response));
     106                                                var html = manejoJsonPDF(JSON.stringify(response));
     107                                                //alert(html);
     108                                                //alert("ver respuesta")
     109                                                document.getElementById("respuesta").innerHTML = html;
     110...
     111}}}