Ajax调用无法在Google小工具中使用

时间:2012-02-27 13:31:05

标签: google-gadget

我有一个转换率脚本,我知道它可以在Google小工具之外完美运行但是我无法弄清楚为什么它在小工具内部无效。

这是我的XML:

<?xml version="1.0" encoding="UTF-8"?>
<Module>
    <ModulePrefs author="Purefx.co.uk" height="280"></ModulePrefs>
    <UserPref name="title" display_name="Widget Title" default_value="Currency Converter"/>
    <UserPref name="color" display_name="Widget color" default_value="Color" datatype="enum">
        <EnumValue value="Color"/>
        <EnumValue value="Black and White"/>
    </UserPref>
    <UserPref name="style" display_name="Widget Style" default_value="Sidebar" datatype="enum">
        <EnumValue value="Sidebar"/>
        <EnumValue value="header/footer"/>
    </UserPref>
    <UserPref name="attribution" display_name="Attribution text" default_value="Purefx" datatype="enum">
        <EnumValue value="Purefx"/>
        <EnumValue value="Foreign Exchange"/>
        <EnumValue value="Currency exchange"/>
    </UserPref> 
    <Content type="html"><![CDATA[

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {

    $('#convert').click(function(){

     //Get all the values
     var amount = $('#amount').val();
     var from = $('#from').val();
     var to = $('#to').val();

     //Make data string
     var dataString = "amount=" + amount + "&from=" + from + "&to=" + to;

         $.ajax({
           type: "POST",
           url: "ajax_converter.php",
           data: dataString,
           success: function(data){
             //Show results div
             $('#results').show();

            //Put received response into result div
             $('#results').html(data);
           }
         });
    });
});
</script>
]]>
</Content>
</Module>

我没有包含内容的html部分或php脚本,因为该部分100%正常工作且与此问题无关。

我认为问题特别是Ajax调用的执行,单击'convert'时,firebug控制台窗口中没有“发布”。

我找不到任何可能暗示我遗漏的东西,所以任何想法都会受到赞赏。

非常感谢提前

1 个答案:

答案 0 :(得分:1)

您无法在小工具内部进行直接调用,因为小工具位于小工具容器内,所有调用都由小工具容器代理。

您必须使用io.makeRequest来获取远程数据。

更多信息,请参阅http://code.google.com/apis/gadgets/docs/remote-content.html