【问题标题】:Windows Phone 7/IE9 jQuery ajax "Access is denied"Windows Phone 7/IE9 jQuery ajax“访问被拒绝”
【发布时间】:2012-01-28 20:46:51
【问题描述】:

我正在尝试在 Windows Phone 7.5 上将以下代码与 jQuery 一起使用。每次我尝试对 xml 发出 ajax 请求时,我都会从错误处理程序中返回“访问被拒绝”。 JSONP 在这种情况下不起作用,不幸的是,因为我需要的数据仅在 XML 中。我不确定如何解决这个问题。

编辑:我应该指出,该代码在 Chrome 和 Safari 上运行良好。但是,我没有要在 IE 上测试的 Windows 机器。 编辑 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); 
       } 
   });
};

获取xml的php代理

<?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;
?>

【问题讨论】:

  • 你在用什么? Phone Gap,在原生应用内制作 webapp 或 webcontrol?
  • 一个只有普通 jQuery 的 webapp。

标签: ajax internet-explorer windows-phone-7 jquery


【解决方案1】:

不知道普通的 JQuery 怎么样,但是当我使用 phonegap 时,我遇到了同样的问题并写了这个:

在发出 AJAX 请求之前,您必须允许跨域请求和核心支持,方法是:

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

这些必须在特定的phonegap函数“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 请求中您必须指定资源的完整路径,例如: 网址:“www/about.txt”,

制作面向 Windows Phone 8 的应用程序,在 AJAX 请求中您不得指定资源的完整路径,例如: 网址:“about.txt”,

2.3.源文件扩展名

小心使用未知扩展名文件,例如模板扩展名 *.tpl 或类似文件。有时 AJAX 不喜欢它们,我建议使用简单的 *.txt 和 *.html 扩展名。

3。获取JSON

不知何故 $.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));
    });

【讨论】:

    猜你喜欢
    • 2011-04-29
    • 1970-01-01
    • 2012-05-01
    • 2014-11-20
    • 1970-01-01
    • 2016-08-13
    • 2012-07-23
    • 2015-07-01
    • 1970-01-01
    相关资源
    最近更新 更多