帮助使用 Mootools 和 JSONP

发布于 2024-11-07 09:38:04 字数 1507 浏览 0 评论 0原文

我很难让它发挥作用。我所需要的只是控制台记录返回的对象。尽管脚本标签被注入到头部,但我在日志中什么也没看到。

JSON:

jsonFeed({
    "results":{
        "loggedin": "No",
        "username": "",
        "company": ""
    }
});

JS:

function jsonFeed() {

}

window.addEvent('domready', function() {

    new Request.JSONP({
        url: <correcturl>,
        onComplete: function(data){
            console.log(data); // Nothing returned

        }
    }).send();

});

非常感谢任何帮助。

更新

我已经删除了顶部的 jsonFeed 函数,并将现有代码更改为:

new Request.JSONP({
    log: true,
    url: loginstatus,
    callbackKey: 'jsonFeed',
    onComplete: function(data){
        console.log(data); // Nothing returned

    }
}).send();

在日志中我得到:

JSONP retrieving script with url:http://thedomain/LoggedStatus.aspx?jsonFeed=Request.JSONP.request_map.request_0
jsonFeed is not defined

在 this 被注入中:

<script type="text/javascript" async="true" src="http://thedomain/LoggedStatus.aspx?jsonFeed=Request.JSONP.request_map.request_0"> 

-- if I expand this I see the JSON --


</script>

所以 a) 我收到 jsonFeed not Defined 错误和 b ) onSuccess 没有触发:(

我真的很感谢你们所有的帮助。如果我错过了重点,我很抱歉 :(

更新

添加:

this.jsonFeed = function(data) {
    console.log(data);
};

..并且它有效。谢谢@Dimitar

我仍然不太明白,但现在它可以工作了,这对解决问题很有帮助。

I am having a really hard time trying to get this to work. All I require is to console log the object that is returned. I see nothing at all in the log although the script tag is getting injected into the head.

JSON:

jsonFeed({
    "results":{
        "loggedin": "No",
        "username": "",
        "company": ""
    }
});

JS:

function jsonFeed() {

}

window.addEvent('domready', function() {

    new Request.JSONP({
        url: <correcturl>,
        onComplete: function(data){
            console.log(data); // Nothing returned

        }
    }).send();

});

Any help is greatly appreciated.

UPDATE

I have removed the jsonFeed function at the top and changed the existing code to:

new Request.JSONP({
    log: true,
    url: loginstatus,
    callbackKey: 'jsonFeed',
    onComplete: function(data){
        console.log(data); // Nothing returned

    }
}).send();

In the log I get:

JSONP retrieving script with url:http://thedomain/LoggedStatus.aspx?jsonFeed=Request.JSONP.request_map.request_0
jsonFeed is not defined

In the this gets injected:

<script type="text/javascript" async="true" src="http://thedomain/LoggedStatus.aspx?jsonFeed=Request.JSONP.request_map.request_0"> 

-- if I expand this I see the JSON --


</script>

so a) I'm getting the jsonFeed not defined error and b) the onSuccess isn't firing :(

I really do appreciate all your help guys. And I am sorry if I am missing the point :(

UPDATE

added:

this.jsonFeed = function(data) {
    console.log(data);
};

.. and it works. Thank you @Dimitar

I still don't quite understand it but now it works it helps when working it out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

过潦 2024-11-14 09:38:04

它不起作用,因为您的回调函数名称会忽略 Request.JSONP 发送的回调函数名称并返回 jsonFeed 。

http://mootools.net/docs/more/Request/Request.JSONP

callbackKey (字符串:默认为回调)服务器用于包装 JSON 结果的 url 中的键。因此,例如,如果您使用了callbackKey: 'callback',那么服务器期望类似 http://..../?q=search+term&callback=myFunction;这必须正确定义。

这是我编写的一个示例类,它从 flickr 获取内容 - 使用自定义回调键 - 很好。 http://fragged.org/mootools-flickr-api-class -via-request-jsonp_1042.html (ps jsfiddle 可能会很慢 atm,周五 13 号的事情!)

另一件事是,如果远程端继续不与您合作并拒绝以正确包装的格式发送数据,例如:

Request.JSONP.request_map.request_0({data})

那么您需要实际确保

this.jsonFeed = function(data) {
    console.log(data);
};

这是在哪里全局对象(例如,窗口) - 你不能限制它,所以要小心函数的定义位置。

如果执行后者,则 jsonFeed 将充当回调 oncomplete 函数的角色。

另一种方法是这样做,它将映射该类定义的本机回调函数并将其导出到您的远程主机喜欢的函数:

onRequest: function() {
    var lastCallback;
    Object.each(Request.JSONP.request_map, function(el) {
        lastCallback = el;
    });
    window.jsonFlickrApi = lastCallback;
},
onComplete: function(data) {
...
}

it does not work because your callback function name ignores the one that Request.JSONP sends and returns jsonFeed instead.

http://mootools.net/docs/more/Request/Request.JSONP

callbackKey (string: defaults to callback) the key in the url that the server uses to wrap the JSON results. So, for example, if you used callbackKey: 'callback' then the server is expecting something like http://..../?q=search+term&callback=myFunction; This must be defined correctly.

here's an example class i wrote that gets stuff off of flickr - who use a custom callback key - it's fine. http://fragged.org/mootools-flickr-api-class-via-request-jsonp_1042.html (p.s. jsfiddle may be slow atm, friday 13th thing!)

the other thing is, if the remote end CONTINUES not to work with you and refuses to send data in the correctly wrapped format, eg:

Request.JSONP.request_map.request_0({data})

then you need to actually make sure that

this.jsonFeed = function(data) {
    console.log(data);
};

where this is the global object (eg, window) - you cannot scope this, so careful where the function is defined.

if doing the latter, jsonFeed will then take the role of a callback oncomplete function.

another way is to do this, which will map the native callback function defined by the class and export it to the one your remote host likes:

onRequest: function() {
    var lastCallback;
    Object.each(Request.JSONP.request_map, function(el) {
        lastCallback = el;
    });
    window.jsonFlickrApi = lastCallback;
},
onComplete: function(data) {
...
}
暖伴 2024-11-14 09:38:04
jsonFeed(
    return //or anything else that will make this piece of data recognizable on your page
    {
    "results":{
        "loggedin": "No",
        "username": "",
        "company": ""
    }
});




new Request.JSONP({
        url: <correcturl>,
        callbackKey: 'jsonFeed'
        onComplete: function(data){
            console.log(data); // Nothing returned

        }
    }).send();
jsonFeed(
    return //or anything else that will make this piece of data recognizable on your page
    {
    "results":{
        "loggedin": "No",
        "username": "",
        "company": ""
    }
});




new Request.JSONP({
        url: <correcturl>,
        callbackKey: 'jsonFeed'
        onComplete: function(data){
            console.log(data); // Nothing returned

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