调用从Firefox扩展$ .ajax不工作 [英] call to $.ajax from firefox extension not working
问题描述
我的问题是,我的Firefox中的$ .ajax扩展名不会被调用。如果我看错误控制台,它会显示一条消息,如不安全的使用Jquery。我尝试过搜索谷歌和其他网站,但我不能提出一个解决方案。
这里的问题的代码:
var listMainVid = function()
{
//创建一个JSONP调用。我们使用的是JSONP而不是JSON,因为我们必须做一个跨域的AJAX调用
$ .ajax({
url:vidinfo_q_url +?jsoncallback =?,//不要忘了把在'jsoncallback ='部分
dataType:'jsonp',//创建一个JSONP请求,将其作为文本接收,并由jQuery解释为JSON:jsonp text xml。
data:
video_url:''+ doc.document.location
},
success:function(data,textStatus,jqXHR)//记住这只是发送成功请求
if(data.status ==='SUCCESS')
{
var vid_loc = data.url,img_url = data.image_url;
if(Object.prototype .toString.call(vid_loc)==='[object Array]')//检查它是否是一个数组
vid_loc = data.url [0];
if(Object.prototype.toString.call(img_url)==='[object Array]')//检查它是否是一个数组
img_url = data.image_url [0];
addVideoToVidDiv(data.id,vid_loc,img_url);
}
else //错误
{
// alert(Error!Data =+ data.status);
}
afterMainVid();
,
error:function(xhRequest,ErrorText,thrownError)
{
Application.console.log(Can not do because:+ ErrorText +,+ thrownError);
afterMainVid();
}
});
afterMainVid();
$ / code>
任何帮助/指针都将不胜感激。
好的,我终于明白了我自己的想法。这可能会遇到同样的问题。改变dataType:'jsonp',到dataType:'json',就是这样!我不知道为什么,但FF似乎不支持从扩展内部的jsonp调用。这里需要注意的一点是,在FF扩展中,无论如何都不需要jsonp,因为扩展可以自由地进行跨域的ajax调用。希望这有助于解决方案。
好的,就像SomeKittens的要求,我回答我自己的问题(不知道我能做什么这个问题的解决方案是改变dataType:'jsonp',到dataType:'json'。
我不知道为什么,但FF似乎不支持从扩展内部的jsonp调用。这里需要注意的一点是,在FF扩展中,无论如何都不需要jsonp,因为扩展可以自由地进行跨域的ajax调用。希望这会有帮助。
我也提供了问题本身的答案。
I am trying to make a firefox extension that will list all the videos on a page. I had already got it working as a normal js script (not as an extension) so I know the script works.
My problem is that the $.ajax inside my firefox extension doesn't get called at all. If I look at the error console it shows a message like "unsafe use of Jquery". I've tried searching Google and other sites but I couldn't come up with a solution.
Here's the code where the problem is:
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();
}
Any help/pointers would be greatly appreciated.
OK, I finally figured it out on my own. This is to anyone else who might run into the same problem. Change the dataType: 'jsonp', TO dataType: 'json', And that's it! I don't know why but FF doesn't seem to support 'jsonp' calls from inside extensions. One thing to note here is that inside FF extensions, you don't need 'jsonp' anyway as the extensions are free to make cross-domain ajax calls. Hope this will help.
OK, as SomeKittens requested, I am answering my own question (didn't know I could do that).
The solution to the problem is to change the dataType: 'jsonp', To dataType: 'json'.
I don't know why but FF doesn't seem to support 'jsonp' calls from inside extensions. One thing to note here is that inside FF extensions, you don't need 'jsonp' anyway as the extensions are free to make cross-domain ajax calls. Hope this will help.
I've also provided the answer in the question itself.
这篇关于调用从Firefox扩展$ .ajax不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!