AJAX - Origin null is not allowed by Access-Control-Allow-Origin

 
Vista:
sin imagen de perfil

Origin null is not allowed by Access-Control-Allow-Origin

Publicado por Borja (1 intervención) el 14/10/2014 13:48:26
Hola, soy novato con JQuery y JQuery Mobile y tengo un problema al acceder a un fichero XML por medio de una llamada AJAX.

Tras intentar varios cambios con respecto al código original y habiendo seguido algún post que indica el mismo problema...el problema persiste.

He probado varias supuestas soluciones:
Añadiendo header('Access-Control-Allow-Origin: *'); en el script
Añadiendo en cordova.xml las lines:
<access origin=".*"/> <!-- Allow all domains, suggested development use only -->
<access subdomains="true" uri="*"/>

(Obtainxml.js)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
 * 
 */
var xml;
$(document).ready(function(){
    $.ajax({
        type: "GET",
        url: "file:///machines_samples.xml",
        data: '',
        dataType: "xml",
        success: xmlParser
    });
});
    //loading XML file and parsing to .main div.
    function xmlParser(data) {
        xml = data;
 
        //$('#load').fadeOut();
 
        $(xml).find("ControlProduccion").each(function (){
            id_maquina = $(this).attr("IdMaquina");
            var name = $(this).find("Nombre").text();
            var estado = $(this).find("Estado").text();
            var id_pieza = $(this).find("IdPieza").text();
            var descripcion_pieza = $(this).find("DescripcionPieza").text();
            var id_OF = $(this).find("idOF").text();
            var descripcion_OF = $(this).find("DescripcionOF").text();
            var descripcion_grabado = $(this).find("DescripcionGrabado").text();
            var operario = $(this).find("Operario").text();
            var NPPlanificadas = $(this).find("NumPiezasPlanificadas").text();
            var NPBuenas = $(this).find("NumPiezasBuenas").text();
            var NPRealizadasChatarra = $(this).find("NumPiezasRealizadasChatarra").text();
            var NPProducidas = $(this).find("NumPiezasProducidas").text();
            var NPPlanificadasMaquina = $(this).find("NumPiezasPlanificadasMaquina").text();
            var NPRealizadasMaquina = $(this).find("NumPiezasRealizadasNC").text();
 
            $("#list").append('<li data-role="list-divider" data-theme="a"><div onclick="alert("Some info...");" class="ui-icon ui-icon-info" style="color:black;float:right"></div></li><li data-role="list-divider" data-theme="a">Lista Máquinas</li> <li><h2>' + id_maquina + '/'+ name + '</h2> <p>' + 'Piezas producidas turno:' + NumPiezasBuenas + '</p><p>' + 'Piezas producidas día:' +  NPRealizadasMaquina + '</p> <p>' + 'Piezas chatarra:' +  NPRealizadasChatarra + '</p></li>');
           // $("#list").append('<li><h3 id="name">' + name + '</h3><ul><li>Email: '+ email + '</li><li>Job Title: '+ jobtitle + '</li><li>Address: '+ address + '</li><li>Work Phone: '+ workphone + '</li><li>Home Phone: '+ homephone + '</li><li>Cell Phone: '+ cellphone + '</li><li>Fax: '+ fax + '</li></ul></li>');
 
//            $('#list').listview('refresh'); 
        });
        $('#list').listview('refresh');
    }



y el XML que leo:
(machines_samples.xml)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<ArrayOfControlProduccion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
 
<ControlProduccion>
<IdMaquina>1</IdMaquina>
<Nombre>Maquina1</Nombre>
<Estado>true</Estado>
<IdPieza>1111</IdPieza>
<DescripcionPieza>Piezas estampadas</DescripcionPieza>
<IdOF>1111111</IdOF>
<DescripcionOF>NI</DescripcionOF>
<DescripcionGrabado>NI</DescripcionGrabado>
<Operario>Borja Hazas</Operario>
<NumPiezasPlanificadas>1000</NumPiezasPlanificadas>
<NumPiezasBuenas>100</NumPiezasBuenas>
<NumPiezasRealizadasChatarra>10</NumPiezasRealizadasChatarra>
<NumPiezasProducidas>2100</NumPiezasProducidas>
<NumPiezasPlanificadasMaquina>3000</NumPiezasPlanificadasMaquina>
<NumPiezasRealizadasNC>2100</NumPiezasRealizadasNC>
</ControlProduccion>
 
<ControlProduccion>
<IdMaquina>2</IdMaquina>
<Nombre>Maquina1</Nombre>
<Estado>true</Estado>
<IdPieza>2222</IdPieza>
<DescripcionPieza>Piezas estampadas</DescripcionPieza>
<IdOF>2222222</IdOF>
<DescripcionOF>NI</DescripcionOF>
<DescripcionGrabado>NI</DescripcionGrabado>
<Operario>Borja Hazas</Operario>
<NumPiezasPlanificadas>1000</NumPiezasPlanificadas>
<NumPiezasBuenas>100</NumPiezasBuenas>
<NumPiezasRealizadasChatarra>10</NumPiezasRealizadasChatarra>
<NumPiezasProducidas>2100</NumPiezasProducidas>
<NumPiezasPlanificadasMaquina>3000</NumPiezasPlanificadasMaquina>
<NumPiezasRealizadasNC>2100</NumPiezasRealizadasNC>
</ControlProduccion>
 
<ControlProduccion>
<IdMaquina>3</IdMaquina>
<Nombre>Maquina1</Nombre>
<Estado>true</Estado>
<IdPieza>3333</IdPieza>
<DescripcionPieza>Piezas estampadas</DescripcionPieza>
<IdOF>3333333</IdOF>
<DescripcionOF>NI</DescripcionOF>
<DescripcionGrabado>NI</DescripcionGrabado>
<Operario>Borja Hazas</Operario>
<NumPiezasPlanificadas>1000</NumPiezasPlanificadas>
<NumPiezasBuenas>100</NumPiezasBuenas>
<NumPiezasRealizadasChatarra>10</NumPiezasRealizadasChatarra>
<NumPiezasProducidas>2100</NumPiezasProducidas>
<NumPiezasPlanificadasMaquina>3000</NumPiezasPlanificadasMaquina>
<NumPiezasRealizadasNC>2100</NumPiezasRealizadasNC>
</ControlProduccion>
 
</ArrayOfControlProduccion>





También añado el Log:
10-14 06:55:14.670: V/WebViewChromium(2001): Binding Chromium to the main looper Looper (main, tid 1) {b3d2af88}

10-14 06:55:14.680: I/chromium(2001): [INFO:library_loader_hooks.cc(112)] Chromium logging enabled: level = 0, default verbosity = 0
10-14 06:55:14.680: I/BrowserProcessMain(2001): Initializing chromium process, renderers=0

10-14 06:55:14.800: E/chromium(2001): [ERROR:gl_surface_egl.cc(153)] No suitable EGL configs found.
10-14 06:55:14.800: E/chromium(2001): [ERROR:gl_surface_egl.cc(620)] GLSurfaceEGL::InitializeOneOff failed.
10-14 06:55:14.810: E/chromium(2001): [ERROR:gl_surface_egl.cc(153)] No suitable EGL configs found.
10-14 06:55:14.810: E/chromium(2001): [ERROR:gl_surface_egl.cc(620)] GLSurfaceEGL::InitializeOneOff failed.
10-14 06:55:14.810: E/chromium(2001): [ERROR:gpu_info_collector.cc(86)] gfx::GLSurface::InitializeOneOff() failed

10-14 06:55:14.850: W/chromium(2001): [WARNING:proxy_service.cc(888)] PAC support disabled because there is no system implementation

10-14 06:55:14.960: D/dalvikvm(2001): GC_FOR_ALLOC freed 60K, 5% free 2884K/3028K, paused 47ms, total 48ms

10-14 06:55:14.960: I/dalvikvm-heap(2001): Grow heap (frag case) to 3.503MB for 635812-byte allocation

10-14 06:55:15.030: D/dalvikvm(2001): GC_FOR_ALLOC freed <1K, 5% free 3505K/3652K, paused 67ms, total 67ms

10-14 06:55:15.130: I/CordovaLog(2001): Changing log level to DEBUG(3)
10-14 06:55:15.130: I/CordovaLog(2001): Found preference for useBrowserHistory=false

10-14 06:55:15.130: D/CordovaLog(2001): Found preference for useBrowserHistory=false

10-14 06:55:15.140: I/CordovaLog(2001): Found preference for UIWebBounce=false

10-14 06:55:15.140: D/CordovaLog(2001): Found preference for UIWebBounce=false

10-14 06:55:15.160: D/DroidGap(2001): DroidGap.init()
10-14 06:55:15.170: D/CordovaWebView(2001): >>> loadUrl(file:///android_asset/www/index.html)
10-14 06:55:15.170: D/PluginManager(2001): init()
10-14 06:55:15.180: D/CordovaWebView(2001): >>> loadUrlNow()

10-14 06:55:15.360: D/SoftKeyboardDetect(2001): Ignore this event

10-14 06:55:15.480: D/gralloc_goldfish(2001): Emulator without GPU emulation detected.
10-14 06:55:15.590: D/SoftKeyboardDetect(2001): Ignore this event

10-14 06:55:16.160: D/DroidGap(2001): onMessage(onPageStarted,file:///android_asset/www/index.html)
10-14 06:55:22.760: D/Cordova(2001): onPageFinished(file:///android_asset/www/index.html)

10-14 06:55:22.770: D/CordovaWebView(2001): >>> loadUrlNow()
10-14 06:55:22.780: D/DroidGap(2001): onMessage(onNativeReady,null)
10-14 06:55:22.780: D/DroidGap(2001): onMessage(onPageFinished,file:///android_asset/www/index.html)
10-14 06:55:23.140: D/DroidGap(2001): onMessage(networkconnection,3g)
10-14 06:55:23.190: D/DroidGap(2001): onMessage(spinner,stop)

10-14 06:55:23.560: D/CordovaLog(2001): XMLHttpRequest cannot load http://127.0.0.1:56151/7f2a9954-26ae-4d56-ab29-b38ea73b5bf4. Origin null is not allowed by Access-Control-Allow-Origin.
10-14 06:55:23.560: D/CordovaLog(2001): file:///android_asset/www/index.html: Line 0 : XMLHttpRequest cannot load http://127.0.0.1:56151/7f2a9954-26ae-4d56-ab29-b38ea73b5bf4. Origin null is not allowed by Access-Control-Allow-Origin.

10-14 06:55:23.560: I/chromium(2001): [INFO:CONSOLE(0)] "XMLHttpRequest cannot load http://127.0.0.1:56151/7f2a9954-26ae-4d56-ab29-b38ea73b5bf4. Origin null is not allowed by Access-Control-Allow-Origin.", source: file:///android_asset/www/index.html (0)

10-14 06:55:23.580: D/CordovaLog(2001): JSCallback Error: Request failed.

10-14 06:55:23.580: D/CordovaLog(2001): file:///android_asset/www/cordova-2.0.0.js: Line 3698 : JSCallback Error: Request failed.
10-14 06:55:23.580: I/chromium(2001): [INFO:CONSOLE(3698)] "JSCallback Error: Request failed.", source: file:///android_asset/www/cordova-2.0.0.js (3698)
10-14 06:55:24.800: D/DroidGap(2001): onMessage(spinner,stop)

10-14 06:55:25.310: I/Choreographer(2001): Skipped 75 frames! The application may be doing too much work on its main thread.
10-14 06:55:26.120: I/Choreographer(2001): Skipped 32 frames! The application may be doing too much work on its main thread.
10-14 06:55:26.650: I/Choreographer(2001): Skipped 33 frames! The application may be doing too much work on its main thread.
10-14 06:55:26.900: I/Choreographer(2001): Skipped 36 frames! The application may be doing too much work on its main thread.

10-14 06:55:27.250: D/dalvikvm(2001): GC_FOR_ALLOC freed 247K, 9% free 3773K/4104K, paused 64ms, total 67ms

10-14 06:55:27.400: I/Choreographer(2001): Skipped 58 frames! The application may be doing too much work on its main thread.
10-14 06:55:27.820: I/Choreographer(2001): Skipped 83 frames! The application may be doing too much work on its main thread.
10-14 06:55:31.080: I/Choreographer(2001): Skipped 48 frames! The application may be doing too much work on its main thread.
10-14 06:55:31.270: I/Choreographer(2001): Skipped 47 frames! The application may be doing too much work on its main thread.
10-14 06:55:31.410: I/Choreographer(2001): Skipped 32 frames! The application may be doing too much work on its main thread.
10-14 06:55:32.230: I/Choreographer(2001): Skipped 31 frames! The application may be doing too much work on its main thread.
10-14 06:55:32.350: I/Choreographer(2001): Skipped 31 frames! The application may be doing too much work on its main thread.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil

Origin null is not allowed by Access-Control-Allow-Origin

Publicado por Alfredo (4 intervenciones) el 15/11/2014 14:57:10
Saludos Borja,

Por razones de seguridad los exploradores no pueden leer archivos de la máquina del cliente mediante javascript a excepción de que uses File API.
Tienes dos opciones:
1. Usar el File API (aqui te dejo un URL donde explican como se usa: http://www.html5rocks.com/en/tutorials/file/dndfiles/)
2. Subir el archivo al servidor y leerlo luego con javascript.

Espero que esto te ayude.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar