jQuery AJAX删除了Phonegap上的Javascript

时间:2012-06-27 22:35:05

标签: javascript jquery ajax security cordova

我正在使用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>

知道为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

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

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

document.write(data);