firefox扩展对$.ajax的调用不起作用

call to $.ajax from firefox extension not working

本文关键字:调用 不起作用 ajax 扩展 firefox      更新时间:2024-01-16

我正在尝试制作一个firefox扩展,它将在一个页面上列出所有视频。我已经把它作为一个普通的js脚本(而不是扩展)来工作了,所以我知道这个脚本是有效的。

我的问题是我的firefox扩展中的$.ajax根本没有被调用。如果我查看错误控制台,它会显示一条类似"不安全地使用Jquery"的消息。我试过搜索谷歌和其他网站,但没能找到解决方案。

以下是问题所在的代码:

    var listMainVid = function ()
{
    // Make a JSONP call. We are using JSONP instead of JSON because we have to make a cross-domain AJAX call
    $.ajax({
        url:        vidinfo_q_url + "?jsoncallback=?",      // Don't forget to put in the 'jsoncallback=' part
        dataType:   'jsonp',        // Make a JSONP request, have it received as text, and interpreted by jQuery as JSON: "jsonp text xml."
        data:       {
                        video_url:  '' + doc.document.location
                    },
        success:    function ( data, textStatus, jqXHR )    // Keep in mind that this is just the request sending success.
                    {
                        if ( data.status === 'SUCCESS' )
                        {
                            var vid_loc = data.url, img_url=data.image_url;
                            if( Object.prototype.toString.call( vid_loc ) === '[object Array]' )    // Check if it's an array
                                vid_loc = data.url[0];
                            if( Object.prototype.toString.call( img_url ) === '[object Array]' )    // Check if it's an array
                                img_url = data.image_url[0];
                            addVideoToVidDiv( data.id, vid_loc, img_url );
                        }
                        else    // Error
                        {
                            //alert ( " Error! Data=" + data.status );
                        }
                        afterMainVid();
                    },  
        error:      function( xhRequest, ErrorText, thrownError )       
                    {
                        Application.console.log( " Can't do because: " + ErrorText + ", " + thrownError );
                        afterMainVid();
                    }
    });
    afterMainVid();
}

如有任何帮助/建议,我们将不胜感激。

好吧,我终于自己想明白了。这是给其他可能遇到同样问题的人的。将dataType:'jsonp'更改为dataType:'sjson',就这样!我不知道为什么,但FF似乎不支持来自内部扩展的"jsonp"调用。这里需要注意的一点是,在FF扩展中,无论如何都不需要"jsonp",因为扩展可以自由地进行跨域ajax调用。希望这会有所帮助。

您是否已完全安装扩展?你不能只执行.xul文件,你必须正确安装它,让Firefox知道你"信任"这个扩展,然后再让它做AJAX请求之类的事情。

好吧,按照SomeKittens的要求,我正在回答我自己的问题(不知道我能做到)。

该问题的解决方案是将dataType:'jsonp'更改为dataType:'sjson'。

我不知道为什么,但FF似乎不支持来自内部扩展的"jsonp"调用。这里需要注意的一点是,在FF扩展中,无论如何都不需要"jsonp",因为扩展可以自由地进行跨域ajax调用。希望这会有所帮助。

我也提供了问题本身的答案。