問題描述
我有一個 web 應用程序(在 java 中),我需要通過 jquery 或簡單的 javaScript ajax 調用在任何瀏覽器中通過 Ajax 調用來限制跨域請求(包括 IE 限制 XDomainRequest 對象).
I have web application (In java) where I need to restrict Cross Domain request through Ajax call in any browser (Including IE restricting XDomainRequest object) through jquery or simple javaScript ajax call.
我的最終目標是在通過某些瀏覽器設置或設置響應標頭進行調用時限制它,這樣它就不會在第一點本身進行調用.
My ultimate aim it to restrict it while the call is made either through some browser setting or setting response header so it wont make the call in the first point itself.
如果同源政策是一種解決方案,請說明它是如何解決的.
If same-orgin policy is a solution pls do explain how it is addressed.
感謝和問候,大洋洲
推薦答案
您可以通過 Access-Control-Allow-Origin 響應頭.如果缺少標頭,則只允許來自同一域的請求.
You can control which domains you accept AJAX requests from via the Access-Control-Allow-Origin response header. If the header is missing, only requests from the same domain are allowed.
更新:如果您想禁止啟動 AJAX 請求,您可以嘗試 X-Content-Security-Policy:
xhr-src 'none'
,但我無法想象會出現(xiàn)什么情況有用.或許你可以更詳細地解釋一下情況?
Update: if you want to disallow even the initiation of AJAX requests, you could try X-Content-Security-Policy:
xhr-src 'none'
, but I can't imagine any situation where that would be useful. Maybe you could explain the situation in a little more detail?
這篇關于限制跨域 Ajax 請求的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!