沒有為 OPTIONS Ajax 請求設置標頭
我有一個
ascx頁面GetToken.ashx。public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.AppendHeader("Access-Control-Allow-Origin", "*"); context.Response.Write(Token.CreateToken()); }當我 AJAX 到這個頁面時,它返回以下標題:
Request Method:GET Status Code:200 OK Access-Control-Allow-Origin:* Cache-Control:private Content-Length:36 Content-Type:text/plain; charset=utf-8 Date:Tue, 14 Apr 2015 17:20:53 GMT Server:Microsoft-IIS/8.5 X-AspNet-Version:4.0.30319 X-Powered-By:ASP.NET當發出 AJAX 請求的頁面放置在沙盒 iFrame 中時,它會顯示錯誤:
XMLHttpRequest cannot load https://127.0.0.1:112/handlers/gettoken.ashx. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.並返回標題:
Request Method:OPTIONS Status Code:200 OK Allow:OPTIONS, TRACE, GET, HEAD, POST Content-Length:0 Date:Tue, 14 Apr 2015 17:30:14 GMT Public:OPTIONS, TRACE, GET, HEAD, POST Server:Microsoft-IIS/8.5 X-Powered-By:ASP.NET我似乎無法獲得
OPTIONS添加標頭的請求。添加allow-same-origin到沙盒屬性會將請求更改為 aGET,但我不希望授予 iFrame 這些權限。
我假設你的意思是寫
ashx,不是ascx。該ProcessRequest (HttpContext context)方法的存在表明它是通用處理程序而不是使用者控制項。我製作了一個非常簡單的頁面來測試:
<%@ Page Language="C#" AutoEventWireup="true" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript" src="Scripts/jquery-1.4.1.js"></script> </head> <body> <div id="testCorsDiv"> </div> <script type="text/javascript"> $.ajax({ type: "GET", url: "/Handler/testCors.ashx", dataType: "text", success: function (theData) { $("#testCorsDiv").text(theData); }, error: function (theData) { alert('error'); } }); </script> <% if(string.IsNullOrEmpty(Request.QueryString["sandboxed"])) { %> <iframe src="http://127.0.0.1:49253/SandboxTest.aspx?sandboxed=true" sandbox="allow-scripts" width="600"> </iframe> <% } %> </body> </html>我載入頁面
http://localhost:49253/SandboxTest.aspx。然後頁面向 div 發出ajax請求http://localhost:49253/Handler/testCors.ashx並將其輸出放入testCorsDivdiv。這會直接生成GET處理程序(因為它來自同一來源)並插入輸出。在頁面中還有一個沙盒
iframe,它使用 url 載入相同的頁面http://127.0.0.1:49253/SandboxTest.aspx。?sandboxed=true用於防止 iframe 遞歸載入內部 iframe 。然後,在 iframe 中載入的頁面將嘗試向其發出 ajax 請求並在其自己的divhttp://127.0.0.1:49253/Handler/testCors.ashx副本中顯示輸出。testCorsDiv只要沙盒 iframe 具有
allow-scripts這種魅力,它就可以發揮作用。iframe生成一個看起來像這樣的OPTIONS請求(來自 Fiddler,用 Chrome 測試):OPTIONS http://127.0.0.1:49253/Handler/testCors.ashx HTTP/1.1 Host: 127.0.0.1:49253 Connection: keep-alive Cache-Control: max-age=0 Access-Control-Request-Method: GET Origin: null User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36 Access-Control-Request-Headers: accept, x-requested-with Accept: */* Referer: http://127.0.0.1:49253/SandboxTest.aspx?sandboxed=true Accept-Encoding: gzip, deflate, sdch Accept-Language: fi-FI,fi;q=0.8,en-US;q=0.6,en;q=0.4然後我的
testCors.ashx處理程序吐出一些標題,說這看起來不錯,然後瀏覽器跟進 aGET並且它可以正常工作。這樣
testCors.ashx做:public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; context.Response.AppendHeader("Access-Control-Allow-Origin", "*"); context.Response.AppendHeader("Access-Control-Allow-Headers", "content-type, x-requested-with, accept"); context.Response.AppendHeader("Access-Control-Allow-Methods", "POST, OPTIONS, GET"); context.Response.Write("Hello World"); }所以我的測試表明應該可以做你想做的事。儘管這可能是一個問題,但如果您的處理程序只能由經過身份驗證/授權的使用者訪問,那麼這可能是一個問題。如您所見,
OPTIONS請求沒有向處理程序發送 cookie。但另一方面,您的問題表明對您的選項請求的響應是Status Code:200.4**我想如果缺少所需的身份驗證cookie ,那將是一些。最後,我真的不知道您的情況出了什麼問題,但也許(?)我的簡單範例頁面可以為您提供一些線索,幫助您自己找到問題。