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

时间:2023-02-06 13:21:29

I have huge problem with loading xml file from assets path by jQuery mobile in phonegap project.

我在phonegap项目中通过jQuery mobile从资源路径加载xml文件时遇到了很大的问题。

I need to load an xml file. File is placed in the root of my project. Problem is with ajax url: "language.xml". Here is my code:

我需要加载一个xml文件。文件放在我的项目的根目录中。问题在于ajax url:“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);
            }
        });
    }
}); 

When I use the absolute path, I am able to load this file adding url: file:///android_asset/www/language.xml

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

This is good only for Android assets. But I need the correct path for iOS too.

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

Is it possible to link URL path absolutly/relativly by jQuery to be able to load file in Android/iOS devices?

UPDATE Code above is correct. Fail was in testing within the desktop browser. Project works fine on Android and iOS with relative paths.

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

There's no need to add absolute path like file:///android_asset/www/ for Android or file:///var/mobile/Applications/7D6D107B-D9DC-479B-9E22-4847F0CA0C40/YourApplication.app/www/ for iOS .

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

2 个解决方案

#1


1  

JQUERY:

JQUERY:

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

PHP CODE:

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:

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');
}   

#2


0  

Try putting your xml file in "www" folder and then you should be possible to access the file with just the filename.

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

Also, see the autocomplete example : http://jqueryui.com/autocomplete/#xml In the demo they have directly accessed "london.xml"

另外,请参阅自动完成示例: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()
  };
 });

Hopefully it will work.

希望它会起作用。

#1


1  

JQUERY:

JQUERY:

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

PHP CODE:

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:

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');
}   

#2


0  

Try putting your xml file in "www" folder and then you should be possible to access the file with just the filename.

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

Also, see the autocomplete example : http://jqueryui.com/autocomplete/#xml In the demo they have directly accessed "london.xml"

另外,请参阅自动完成示例: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()
  };
 });

Hopefully it will work.

希望它会起作用。