304 CORS_html/css_WEB-ITnose

WBOY
WBOYオリジナル
2016-06-24 11:42:451259ブラウズ

304 レスポンス、CORS 問題: Access-Control-Allow-Origin ヘッダー情報がない場合、最後に返された 200 リクエストが優先されます。

例: 削除された可能性があります
http://7af3zm.com1.z0.glb.clouddn.com/ajax_304_cors.html

添付されたソースコード:
html

<!doctype html><html><head>	<meta charset="UTF-8"></head><body><script type="text/javascript">// var _ajax_34re = false;	function getAjax () {		var _ajax_34re = false;		if (_ajax_34re) {			return _ajax_34re;		}		try {			_ajax_34re = new XMLHttpRequest();	   } catch (trymicrosoft) {	     try {			_ajax_34re = new ActiveXObject("Msxml2.XMLHTTP");	     } catch (othermicrosoft) {	       try {			_ajax_34re = new ActiveXObject("Microsoft.XMLHTTP");	       } catch (failed) {			_ajax_34re = false;	       }	     }	   }		return _ajax_34re;	}	function load(url) {		request =  getAjax ();		request.open("GET", url);    	request.onreadystatechange = updatePage;    	request.send(null);   }   function updatePage() {     if (request.readyState == 4)       if (request.status == 200)         alert("Server is done!" + request.responseText);       else if (request.status == 404)         alert("Request URL does not exist");       else         alert("Error: status code is " + request.status);   }</script><pre class="brush:php;toolbar:false">	access: 200、304响应都有 Access-Control-Allow-Origin:*;	access-non: 200、304响应都 没 有 Access-Control-Allow-Origin;	access-200: 200响应都有 Access-Control-Allow-Origin:*, 304响应 没 有 Access-Control-Allow-Origin;

clojure

(ns base-web.handler  (:require [compojure.core :refer :all]            [compojure.route :as route]            [ring.middleware.defaults :refer [wrap-defaults site-defaults]]            [clojure.pprint :as log]))(def my-route    (-> site-defaults        (assoc-in [:security :anti-forgery] false)        (assoc-in [:params] false)        ))(defn print-req [req]    (println )    (log/pprint my-route)    (log/pprint req)    (log/pprint (slurp (:body req)))    (println "done"))(defn access-200 [req]    (let [etag "e-access-200"]    (if (not= etag (get-in req [:headers "if-none-match"]))        (do (println "200")            {:status 200,             :headers {"Content-Type" "application/json", "Access-Control-Allow-Origin" "*", "ETag" etag}             :body "{\"success\":true, \"frame\": \"ring\"}"})         (do (println "304")            {:status 304,                :headers {"Content-Type" "application/json", "ETag" etag}}))))(defn access-non [req]    (let [etag "e-access-non"]    (if (not= etag (get-in req [:headers "if-none-match"]))        (do (println "200")            {:status 200,             :headers {"Content-Type" "application/json", "ETag" etag}             :body "{\"success\":true, \"frame\": \"ring\"}"})         (do (println "304")            {:status 304,                :headers {"Content-Type" "application/json", "ETag" etag}}))))(defn access [req]    (let [etag "e-access"]    (if (not= etag (get-in req [:headers "if-none-match"]))        (do (println "200")            {:status 200,             :headers {"Content-Type" "application/json", "Access-Control-Allow-Origin" "*", "ETag" etag}             :body "{\"success\":true, \"frame\": \"ring\"}"})         (do (println "304")            {:status 304,                :headers {"Content-Type" "application/json", "ETag" etag}}))))(defroutes app-routes  (GET "/access-200" req (access-200 req) )  (GET "/access-non" req (access-non req) )  (GET "/access" req (access req) )  (ANY "*" req    (print-req req)    "{\"success\":true, \"frame\": \"compojure\"}")  (route/not-found "Not Found"))(def app  (wrap-defaults app-routes my-route));; lein ring server [<port>]

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。