如何让jQuery ajax调用从greasemonkey页面事件中工作 [英] How to get jQuery ajax calls to work from greasemonkey page events
问题描述
我有一个GM脚本,它在页面上插入一个链接,然后在点击它时添加一个事件监听器。
I have a GM script which inserts a link on to the page, and then adds an event listener for when its clicked.
然后运行一个包含的函数除了其他一些jQuery.get调用。然而,除非我使用jQuery的unsafeWindow版本,否则这些调用似乎不会触发。
This then runs a function which contains among other things some jQuery.get calls. These calls however do not seem to fire unless I use the unsafeWindow version of jQuery.
function runMyFunc() {
console.log('myFunc is called');
$.get('index.php',function () {
console.log('jQuery.get worked');
});
}
$("#someHook").before('<a id="myLink">Link</a>');
$('#myLink').click(runMyFunc);
以上输出'myFunc被调用'到控制台,但不会对.get做任何事情
The above will output 'myFunc is called' to console, but will not do anything with the .get
我正在使用FF17和GM1.5,jquery来自 http://code.jquery.com/jquery.js
I'm using FF17 and GM1.5, jQUery from http://code.jquery.com/jquery.js
有没有比使用unsafeWindow更好的方法?我在GM 1.0之前有这个工作并且有很多$ .get我需要在我的脚本中更改,而不是所有这些都是从相同的场景运行
Is there a nicer way of getting this to work than using unsafeWindow? I had this working before GM 1.0 and have a lot of $.get which I would need to change in my scripts, not all of which are run from the same scenario
推荐答案
您的代码对我来说很好。 为什么/如何您认为 $。get
无效?
Your code works fine for me. Why/How do you think $.get
is not working?
请记住如果存在服务器错误(404等)且 index.php $ c,您将永远不会看到
'jQuery.get working'
消息$ C>。您是否检查了Firebug Net 面板或 Wireshark 等,以查看是否进行了AJAX调用?
Remember that you will never see the 'jQuery.get worked'
message if there is a server error (404 etc.) with index.php
. Did you check the Firebug Net panel, or Wireshark, etc. to see if the AJAX call was made?
无论如何,如果你安装了这个Greasemonkey脚本,你可以看到代码正常工作,加上一些错误处理:
Anyway, you can see that code working, plus some error handling if you install this Greasemonkey script:
// ==UserScript==
// @name _delme9h762
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @include http://fiddle.jshell.net/ZqhRH/*
// @require http://code.jquery.com/jquery.js
// @grant GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
function runMyFunc() {
console.log('myFunc is called');
$.get('index.php', function () {
console.log ('jQuery.get worked');
} )
.error ( function (respObj) {
console.log ("Error! ", respObj.status, respObj.statusText);
} )
.complete ( function (respObj) {
console.log ("AJAX Complete. Status: ", respObj.status);
} )
;
}
$("#someHook").before('<a id="myLink">Link</a>');
$('#myLink').click(runMyFunc);
然后访问 fiddle.jshell.net/ZqhRH/1/show/ 。
And then visit fiddle.jshell.net/ZqhRH/1/show/.
注意,控制台将显示:
调用myFunc
错误! 404 NOT FOUND
AJAX完成。状态:404
myFunc is called
Error! 404 NOT FOUND
AJAX Complete. Status: 404
在jsFiddle网站上,因为 index.php
不存在,但 $。get()
在其他方面完美运作。
On the jsFiddle site, because index.php
does not exist there, but $.get()
is otherwise working perfectly.
这篇关于如何让jQuery ajax调用从greasemonkey页面事件中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!