JSONP回调方法未定义
我正在尝试在 Greasemonkey 脚本中使用 jquery 获取 jsonp 回调。这是我的 jquery:
$.ajax({
url: "http://mydomain.com/MyWebService?callback=?",
data: { authkey: "temphash" },
type: "get",
dataType: "json",
cache: false,
success: function(data) {
console.log(data);
}
});
在我的 Web 服务 (asp.net) 中,我返回内容类型为 application/javascript
的响应。服务器实际发回的响应是:
jsonp1276109314602({"message":"I'm getting tired of this not working"})
jsonp1276109314602
方法名称由 jquery 随机生成,我用 Request.QueryString["callback"]
获取它
然而,我的成功函数从未被调用,并且 Firebug 控制台给我一个错误,指出 jsonp1276109314602 is not Defined
。
我做错了什么?
注意 我正在通过 craigslist 页面上的 Greasemonkey 脚本进行此调用。这是一个跨域请求,但我可以看到该请求实际上正在发送到服务器并返回一个良好的响应,但无论出于何种原因,当响应返回时,jquery 创建的已注册回调似乎不存在。如果我从 craigslist 页面在 firebug 控制台中运行脚本,它可以正常工作,但如果从 Greasemonkey 脚本运行它,则不行。
I'm trying to get a jsonp callback working using jquery within a greasemonkey script. Here's my jquery:
$.ajax({
url: "http://mydomain.com/MyWebService?callback=?",
data: { authkey: "temphash" },
type: "get",
dataType: "json",
cache: false,
success: function(data) {
console.log(data);
}
});
in my webservice (asp.net) I'm returning the response with a content type of application/javascript
. The response the server is actually sending back is:
jsonp1276109314602({"message":"I'm getting tired of this not working"})
The jsonp1276109314602
method name is being randomly generated by jquery, and I'm grabbing it with Request.QueryString["callback"]
However my success function is never called and the firebug console gives me an error saying jsonp1276109314602 is not defined
.
What am I doing wrong?
NOTE
I'm making this call from a greasemonkey script on a craigslist page. It's a cross-domain request, but I can see the request is actually making it to the server and returning a good response, but for whatever reason the registered callback that jquery creates appears to not exist when the response comes back. It works fine if I run the script in the firebug console from the craigslist page, but not when it's run from the greasemonkey script.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否已经尝试过:
来自 docs:
我没有检查 jQuery 的源代码,但除非您为 dataType 选项指定 jsonp,否则回调函数可能不会创建。
Have you already tried:
From docs:
I haven't examined jQuery's source code but it's possible that the callback function isn't created unless you specify jsonp for the dataType option.
事实证明,您必须做一些额外的事情(双关语)才能让它在 GreetMonkey 脚本中工作。
长答案可以在这里找到:在greasemonkey用户脚本中的jQuery.getJSON。
简短的答案是放弃 JSONP 方法并将其包含在您的脚本中:
不确定我是否完全理解,但它的工作方式就像冠军一样,我能够从我的脚本发出跨域请求。
It turns out that you have to do some extra monkeying around (pun intended) to get it to work inside a greasemonkey script.
The long answer can be found here: jQuery.getJSON inside a greasemonkey user script.
The short answer is to ditch the JSONP approach and include this in your script:
Not sure I understand it all, but nonetheless it works like a champ and I'm able to make cross domain requests from my script.
我认为你应该使用“jsonpCallbackString”而不是“成功”。
http://api.jquery.com/jQuery.ajax/
I think you're supposed to use 'jsonpCallbackString' instead of 'success'.
http://api.jquery.com/jQuery.ajax/