无法从资产路径中的phonegap项目中使用jquery加载xml文件

时间:2012-09-21 19:43:09

标签: android jquery ios cordova jquery-mobile

在phonegap项目中通过xml移动设备从assets路径加载jQuery文件时遇到了很大问题。

我需要加载xml文件。文件位于我项目的 root 中。问题在于ajax网址:“language.xml”。这是我的代码:

var language = 'english';
var regEx = /(\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)/;
$.ajax({
    url: "language.xml",
    success: function(xml) {
        $(xml).find('translation').each(function(){
            var id = $(this).attr('id');
            var text = $(this).find(language).text();
            if(text.match(regEx)){
              $("." + id).replaceWith('<a href="mailto:'+text+'" data-role="button" data-inline="true" data-theme="d" rel="external" data-mini="true">'+text+'</a>');
            }
            else{
              $("." + id).html(text);
            }
        });
    }
}); 

当我使用绝对路径时,我可以加载此文件添加网址:file:///android_asset/www/language.xml

这仅适用于Android资产。但我也需要 iOS 的正确路径。

是否可以通过jQuery绝对地/相对地链接URL路径以便能够在Android / iOS设备中加载文件?

更新 上面的代码是正确的。失败是在桌面浏览器中进行测试。项目在Android和iOS上使用相对路径正常工作。

无需为Android添加file:///android_asset/www/或iOS版file:///var/mobile/Applications/7D6D107B-D9DC-479B-9E22-4847F0CA0C40/YourApplication.app/www/等绝对路径。

2 个答案:

答案 0 :(得分:1)

JQUERY:

    $("#inputString").live("keyup", function(e) 
    {
       $.post("FILE.PHP",{ char:$("#inputString").val() },
         function(data){},'json');
    });

PHP代码:

<?php
$sql = "SELECT `name` FROM `list` WHERE `name` LIKE '%{$_POST['queryString']}%'";
        $query=mysql_query($sql);
        if($result) 
        {
        foreach ($result as $value) echo '<li onClick="fill(\''.$value['name'].'\');">'.$value['name'].'</li>';
        } 
}
?>

CSS:

#autoSuggestionsList {
    position: absolute;
    margin: 30px 4px;
    width: 175px;
    padding: 0;
    font-size: 11px;
    color: #000;
    border-radius: 5px;
    background: #c3d9ff; /* Old browsers */
    background: -moz-linear-gradient(top, #c3d9ff 0%, #b1c8ef 41%, #98b0d9 100%); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#c3d9ff), color-stop(41%,#b1c8ef), color-stop(100%,#98b0d9)); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, #c3d9ff 0%,#b1c8ef 41%,#98b0d9 100%); /* Chrome10+,Safari5.1+ */
    background: -o-linear-gradient(top, #c3d9ff 0%,#b1c8ef 41%,#98b0d9 100%); /* Opera 11.10+ */
    background: -ms-linear-gradient(top, #c3d9ff 0%,#b1c8ef 41%,#98b0d9 100%); /* IE10+ */
    background: linear-gradient(to bottom, #c3d9ff 0%,#b1c8ef 41%,#98b0d9 100%); /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#c3d9ff', endColorstr='#98b0d9',GradientType=0 ); /* IE6-9 */
}
.suggestionList {
    margin: 0px;
    padding: 0px;
}
.suggestionList li {
    list-style: none;
    direction: rtl;
    text-align: right;
    margin: 0px 0px 0px 0px;
    padding: 3px;
    cursor: pointer;
}
.suggestionList li:hover {
    background-color: #659CD8;
    color: #fff;
}
#autoSuggestionsList li:first-child {
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
    border-radius: 5px 5px 0px 0px;
    behavior: url('./css/PIE.htc');
}
#autoSuggestionsList li:last-child {
    border-bottom-left-radius: 5px;
    border-bottom-right-radius: 5px;
    border-radius: 0px 0px 5px 5px;
    behavior: url('./css/PIE.htc');
}   

答案 1 :(得分:0)

尝试将您的xml文件放在“www”文件夹中,然后您应该可以使用文件名访问该文件。

另请参阅自动填充示例:http://jqueryui.com/autocomplete/#xml 在演示中,他们直接访问了“london.xml”

$.ajax({
  url: "london.xml",
  dataType: "xml",
  success: function( xmlResponse ) {
  var data = $( "geoname", xmlResponse ).map(function() {
  //alert($('name', this).text());
  return {
    value: $( "name", this ).text() + ", " +
            ( $.trim( $( "countryName", this ).text() ) || "(unknown country)" ),
    id: $( "geonameId", this ).text()
  };
 });

希望它能奏效。

相关问题