| 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 | }}} |
| 84 | Fuente: [https://www.html5rocks.com/en/tutorials/cors/] |
| 85 | |
| 86 | |
| 87 | En 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 | }}} |