問題描述
在我的 Tizen 系統應用程序中,我發出了一個跨源 AJAX 請求,它在 Tizen 的瀏覽器中運行良好,但是當我將其打包為 Web 應用程序時,跨源請求無法運行.我已經在模擬器和真實設備上對其進行了測試.我也使用 webkit 檢查器記錄網絡日志,截圖如下:
In my application for Tizen system, I make a cross origin AJAX request, it works well in Tizen's browser, but when I package it as web application, the cross origin request can't work. I've tested it both on emulator and real device. I also use the webkit inspector record the network log, the screenshot is as following:
有專家能告訴我為什么嗎?
Could any expert tell me why?
以下是我的代碼:
var url = "";//this is assigned a domain which supports cross domain access according to HTML5 specification.
var client = new XMLHttpRequest();
client.open("GET", url, true);
client.setRequestHeader("Accept-Language", 'en-us');
client.onreadystatechange = function() { alert("succeed"); }
client.send();
推薦答案
任何在外部訪問的資源都應該聲明(見訪問外部網絡資源):
Any resource that it is accessed outside should be declared(see Accessing External Network Resources):
默認情況下您無法訪問外部網絡資源(WARP:W3C訪問請求策略).因此,您必須請求權限小部件來檢索網絡資源.您可以通過以下方式輸入多個 URL使用訪問"選項卡上的添加"按鈕.對于每個 URL,您可以指定如果您想允許小部件訪問 URL 子域.這允許子域欄內容可以通過鼠標點擊切換.
You cannot access external network resources by default (WARP: W3C Access Requests Policy). So, you must request permissions for the widget to retrieve network resources. You can enter several URLs by using the Add button on the Access tab. For each URL, you can indicate if you want to allow the widget to access the URL sub-domains. The Allow subdomain column contents can be toggled by mouse clicks.
所以如果沒有訪問我們在 config.xml
中定義的案例所需的特定資源,它就無法工作:
so it cannot work without having access to the specific resource needed in our case defined in config.xml
:
<access origin="http://url_resource" subdomains="true"/>
或
<access origin="*" subdomains="true"/>
讓一切都過去.
這篇關于Web 應用程序中的跨源請求的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!