簡體   English   中英

Windows Phone 7 / IE9 jQuery ajax“訪問被拒絕”

[英]Windows Phone 7/IE9 jQuery ajax “Access is denied”

我試圖在Windows Phone 7.5上的jQuery中使用以下代碼。 每次我嘗試對xml提出ajax請求時,都會從錯誤處理程序中返回“訪問被拒絕”。 不幸的是,JSONP無法在這種情況下工作,因為我需要的數據僅是XML。 我不確定如何解決此問題。

編輯:我應該指出,該代碼在Chrome和Safari上運行良好。 我沒有Windows計算機可以在IE上進行測試。 編輯2:在IE9上測試,但有相同的錯誤。

javascript:

function loadData(index) {
$.support.cors = true;
$.mobile.allowCrossDomainPages = true;
   $.ajax({
       url: "http://foo.bar/some.xml",
       dataType: "xml",
       success: parseData,
       error: function(XMLHttpRequest, textStatus, errorThrown) { 
         alert("Status: " + textStatus); alert("Error: " + errorThrown); 
       } 
   });
};

PHP代理來獲取XML

<?php
header('Content-type: text/xml');
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS");

$intme = date('YmdHis');
$start = $_GET['ind'];
$url = "http://some.data.source/data.xml";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
echo $output;
?>

不知道普通的JQuery怎么樣,但是當我使用phonegap時,我遇到了同樣的問題並編寫了此代碼:

在發出AJAX請求之前,您必須通過設置以下內容來允許跨域請求和核心支持:

jQuery.support.cors = true;
$.mobile.allowCrossDomainPages = true;

這些必須在特定的電話間隙功能“ DeviceReady”中設置,例如:

document.addEventListener('deviceready', function () {
            jQuery.support.cors = true;
            $.mobile.allowCrossDomainPages = true;
            $.ajax({
                url: "www/about.txt",
                dataType: 'text'
            }).done(function (result) {
                    alert(result);
                });
            });

2.2。 網址

制作面向Windows Phone 8的應用程序時,必須在AJAX請求中指定資源的完整路徑,例如:url:“ www / about.txt”,

制作面向Windows Phone 8的應用程序時,不得在AJAX請求中指定資源的完整路徑,例如:url:“ about.txt”,

2.3。 源文件擴展名

請小心使用未知的擴展名文件,例如模板擴展名* .tpl或類似文件。 有時AJAX不喜歡它們,我建議使用簡單的* .txt和* .html擴展名。

3. getJSON

$ .getJSON以某種方式在Windows Phone上不起作用,例如:

 $.getJSON('www/jsonfiles/jsonfile.txt',
              function(data, status, jqXHR) {
                if(status == "success") {
                    alert(data);
                }
              });

您可以將其替換為AJAX請求,如下所示:

 $.ajax({
        url: 'www/jsonfiles/jsonfile.txt',
        dataType: 'text'
    }).done(function (result) {
        Alert( JSON.parse(result));
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM