JSONP回调方法未定义

发布于 2024-09-04 14:05:41 字数 951 浏览 1 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一口甜 2024-09-11 14:05:41

您是否已经尝试过:

$.ajax({
    url: "http://mydomain.com/MyWebService",
    data: { authkey: "temphash" },
    type: "get",
    dataType: "jsonp",
    cache: false,
    success: function(data) {
        console.log(data);
    }
});

来自 docs

“jsonp”:使用 JSONP 加载 JSON 块。会额外添加一个“?callback=?”添加到 URL 末尾以指定回调。

我没有检查 jQuery 的源代码,但除非您为 dataType 选项指定 jsonp,否则回调函数可能不会创建。

Have you already tried:

$.ajax({
    url: "http://mydomain.com/MyWebService",
    data: { authkey: "temphash" },
    type: "get",
    dataType: "jsonp",
    cache: false,
    success: function(data) {
        console.log(data);
    }
});

From docs:

"jsonp": Loads in a JSON block using JSONP. Will add an extra "?callback=?" to the end of your URL to specify the callback.

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.

旧时浪漫 2024-09-11 14:05:41

事实证明,您必须做一些额外的事情(双关语)才能让它在 GreetMonkey 脚本中工作。

长答案可以在这里找到:在greasemonkey用户脚本中的jQuery.getJSON。

简短的答案是放弃 JSONP 方法并将其包含在您的脚本中:

// @require http://courses.ischool.berkeley.edu/i290-4/f09/resources/gm_jq_xhr.js

不确定我是否完全理解,但它的工作方式就像冠军一样,我能够从我的脚本发出跨域请求。

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:

// @require http://courses.ischool.berkeley.edu/i290-4/f09/resources/gm_jq_xhr.js

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.

倾听心声的旋律 2024-09-11 14:05:41

我认为你应该使用“jsonpCallbackString”而不是“成功”。

http://api.jquery.com/jQuery.ajax/

I think you're supposed to use 'jsonpCallbackString' instead of 'success'.

http://api.jquery.com/jQuery.ajax/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文