0

我正在使用 jQuery 和 Phonegap 编写应用程序

在我的 javascript 代码中,我调用 $.ajax 来向我的 Web 服务器请求应用程序的某些部分。这些部分基本上是我插入到 DOM 中的 html 片段。这些片段包含与该部分本身相关的 javascript 代码(这是动态构建的,这就是为什么我不将它包含在应用程序的主 js 文件中)

我使用以下代码调用这些部分:

//...
$.ajax(
            url,
            {
                type: 'GET',
                success: function(data){
                    $('#content').html(data);
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    //...
                }
            });
//...

从 Web 服务器返回的数据类似于:

<div>...<div>
<script type="text/javascript">
$(document).ready(function() {
   //some code to be executed when this section loads
   //this is dynamically generated
});
</script>

现在,问题是当这段代码执行时,它没有将 SCRIPT 标记插入 DOM(只有 html 部分)。

这在 Chrome 上运行得很好,但是当我在模拟器上测试它(使用 Ripple)时却没有。

我认为这可能是一项安全检查,但对此不确定。

我的 config.xml 有这个:

<?xml version="1.0" encoding="UTF-8"?>
<widget xmlns="http://www.w3.org/ns/widgets" 
        xmlns:gap = "http://phonegap.com/ns/1.0" 
        version="0.1.1"
        versionCode="1"
        id="com.company.prod">  
    <name>...</name>
    <author>...</author>
    <description>...</description>



    <gap:splash src="splash.png" />

    <content src="index.html"/>
    <access uri="*" subdomains="true"/>

    <feature name="http://api.phonegap.com/1.0/battery"/> 
    <feature name="http://api.phonegap.com/1.0/geolocation"/> 
    <feature name="http://api.phonegap.com/1.0/network"/>
    <feature name="http://api.phonegap.com/1.0/notification"/>

</widget>

知道为什么会这样吗?

4

1 回答 1

0

如果我没记错的话,这是因为您不能附加或使用 innerHTML 来插入脚本标签。我建议将此 ajax 调用的位置更改为将进行插入的位置,然后更改行

$('#content').html(data);

document.write(data);
于 2012-06-27T22:43:20.537 回答