PHP Rocks
CORS not working - Druckversion

+- PHP Rocks (https://www.php-rocks.de)
+-- Forum: HTML, CSS, Usability und Barrierefreiheit (https://www.php-rocks.de/https://www.php-rocks.de/forum/11-html-css-usability-und-barrierefreiheit.html)
+--- Forum: JavaScript / JavaScript Frameworks / Ajax (https://www.php-rocks.de/https://www.php-rocks.de/forum/13-javascript-javascript-frameworks-ajax.html)
+--- Thema: CORS not working (/https://www.php-rocks.de/thema/79-cors-not-working.html)



CORS not working - Till - 11.01.2016

Server:
PHP-Code:
header("Access-Control-Allow-Credentials: true");        
header("Access-Control-Allow-Orgin: ".((isset($_SERVER['HTTP_ORIGIN'])) ? $_SERVER['HTTP_ORIGIN'] : "*"));
header("Access-Control-Allow-Headers: X-Requested-With"); 

Client:
Code:
$.ajax( {
               
              
                        url: url,    
                        crossDomain: true,
                        headers: {'X-Requested-With': 'XMLHttpRequest'},
                        type: 'GET',  
                        dataType: 'XML',
                        data: {}  
                } )
                .done(function(response) {
                    $scope.config = $.parseXML(response);
                    
                    alert(JSON.stringify($scope.config));
                    
                })
               .fail(function(jqXHR, textStatus) {
                    console.error('Error: ' + jqXHR.status);
               })
              .always(function() {
                      
              });               
  
        }

Request:
Code:
OPTIONS /cdn/frdl/flow/components/webfan/tmock/config.xml HTTP/1.1
Host: frdl.webfan.de
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Access-Control-Request-Method: GET
Access-Control-Request-Headers: x-requested-with
Origin: http://shell.frdl.de
Connection: keep-alive

Response:
Code:
HTTP/1.1 200 OK
Date: Mon, 11 Jan 2016 18:37:56 GMT
Server: Apache/2.2.16 (Debian)
Accept-Ranges: bytes
Cache-Control: public
Pragma: public
Expires: Wed, 30 Mar 2016 22:59:42 GMT
Etag: "3eb5eee2-0ab1-5174-8183-ce2237626fb1"
X-Frdl-Stability: dev; v3.0.1-beta
X-Cache: cdn://de.webfan.frdl/0.0.1/frdl.webfan.de//cdn/frdl/flow/components/webfan/tmock/config.xml/; pragma=public
Age: 1452537476
Connection: Keep-Alive, Keep-Alive
access-control-allow-credentials: true
Access-Control-Allow-Orgin: http://shell.frdl.de
Access-Control-Allow-Headers: X-Requested-With
Content-Length: 6005
Last-Modified: Thu, 31 Dec 2015 22:59:42 GMT
Keep-Alive: timeout=15, max=100
Content-Type: application/xml

Was mich wundert ist beim request das OPTIONS, ich habe doch als type explizit GET angegeben?
Die gleichen Response header scheinen mit jquery JSONP zu funktionieren, ich möchte hier aber ein .xml Dokument crossdomain laden.

Ich habe schon getest mit crossDomain:false und mitohne X-Requested-With header...

Bestimmt hat das jemand so schonmal gemacht?

Achja, Fehlermeldung:
Zitat:Quellübergreifende (Cross-Origin) Anfrage blockiert: Die Gleiche-Quelle-Regel verbietet das Lesen der externen Ressource auf http://frdl.webfan.de/cdn/frdl/flow/components/webfan/tmock/config.xml. (Grund: CORS-Kopfzeile 'Access-Control-Allow-Origin' fehlt).
Ich sehe aber diese Zeile in meinen headers?

---
Eine andere Frage: Der jquery ajax request sendet keine Cache headers, also Modified-Since und ETag und so?
---

Ich wollte es erstmal mit jquery versuchen, um zu sehen ob es erstmal so funktioniert, lieber wäre mir eigentlich wenn ich es mit einem angular promise realisieren könnte...

---
Oder kann ich mein Vorhaben gleich vergessen (XMLHttpRequest != JSOPN)?


RE: CORS not working - Arne Drews - 12.01.2016

Hi Till,

Zitat:
PHP-Code:
header("Access-Control-Allow-Orgin: ".((isset($_SERVER['HTTP_ORIGIN'])) ? $_SERVER['HTTP_ORIGIN'] : "*")); 
Da fehlt für mein Verständnis ein i bei Origin?!


RE: CORS not working - Till - 12.01.2016

Yo, das wars! Wie peinlich.
Danke!!!


RE: CORS not working - Till - 16.02.2016

RE-Open:
- Nochmal dazu: Das CORS funktioniert jetzt wie gewünscht, JEDOCH NICHT IM IE!

Der IE scheint JEGLICHE Cross-Domain Anfrage zu blocken???

Hat jemand einen Hinweis?

Viele Grüße
Till


RE: CORS not working - Arne Drews - 16.02.2016

Hi Till,

Evtl. der SmartScreen Filter aktiv?

Gruß Arne


RE: CORS not working - Till - 16.02.2016

Ja, SmartScreen Filter war aktiviert.
Nach Deaktivierung erhalte ich eine weitere, andere Fehlermeldung.

mom, ...


RE: CORS not working - Till - 16.02.2016

Sorry, ich denke es lag an einem anderen Fehler (*), der Cross domain content wird nun geladen.

(*) der andere Fehler kam übrigens (im IE) von:

new RegExp(/.+\/(.*?):\d+(:\d+)*$/).exec(new Error().stack.trim()))


RE: CORS not working - Till - 17.02.2016

Der Vollständigkeit halber, da ich hiermit Probleme hatte:
Zitat:To deal with caches where you are not in control, it’s important that Vary contains Origin.
When using cors from origin-different domains subsequently and the Request/Origin is cached by Browser this could be the fix:
PHP-Code:
header("Vary: Origin"); 



RE: CORS not working - Arne Drews - 17.02.2016

Danke für's Posten Deiner Lösung.