在静态 FBML 中加载页面时执行脚本
如何在 FBML 静态页面内的页面加载时执行脚本?
我想要做的是在页面加载并显示一些数据时对我的应用程序进行 ajax 调用。
我尝试了以下脚本,但它不起作用。我没有收到任何消息,既没有成功也没有错误
<script type="text/javascript"><!--
var ajax = new Ajax();
ajax.responseType = Ajax.JSON;
ajax.ondone = function(data) {
new Dialog().showMessage("Success", "Got Response");
}
ajax.onerror = function(data) {
new Dialog().showMessage("Error", "Failure");
}
ajax.requireLogin = false;
ajax.post('http://www.xyz.com/abc');
//--></script>
如果我将其设为一个函数并通过单击调用它,它就可以工作,并且我收到一条成功消息
<a href="#" onclick="on_load(); return false;">Click</a>
<script type="text/javascript"><!--
function on_load() {
var ajax = new Ajax();
ajax.responseType = Ajax.JSON;
ajax.ondone = function(data) {
new Dialog().showMessage("Success", "Got Response");
}
ajax.onerror = function(data) {
new Dialog().showMessage("Error", "Failure");
}
ajax.requireLogin = false;
ajax.post('http://www.xyz.com/abc');
}
//--></script>
How can I execute script on page load inside a FBML Static page?
What I want to do is make an ajax call to my application when the page loads and display some data.
I tried following script but it doesn't work. I don't get any message neither Success nor Error
<script type="text/javascript"><!--
var ajax = new Ajax();
ajax.responseType = Ajax.JSON;
ajax.ondone = function(data) {
new Dialog().showMessage("Success", "Got Response");
}
ajax.onerror = function(data) {
new Dialog().showMessage("Error", "Failure");
}
ajax.requireLogin = false;
ajax.post('http://www.xyz.com/abc');
//--></script>
If I make it a function and call it through on click it works, and I get a success message
<a href="#" onclick="on_load(); return false;">Click</a>
<script type="text/javascript"><!--
function on_load() {
var ajax = new Ajax();
ajax.responseType = Ajax.JSON;
ajax.ondone = function(data) {
new Dialog().showMessage("Success", "Got Response");
}
ajax.onerror = function(data) {
new Dialog().showMessage("Error", "Failure");
}
ajax.requireLogin = false;
ajax.post('http://www.xyz.com/abc');
}
//--></script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用 FBJS(JS 的 facebook 版本)。您可以在此处找到有关ajax调用的文档,
它类似于
UPDATE
添加下一行只是为了确保这不是服务器错误和响应类型。
你在使用FireBug吗?您是否看到对服务器的调用或任何 JS 错误?
更新
如果问题是在页面上传事件上运行它。您应该将脚本放在页面的末尾。就像此处所描述的那样。
You should use FBJS (facebook version of JS). You can find documentation about ajax call here
it will be something like
UPDATE
try to add next lines just to be sure that this is not a server error and a response type.
Are you using FireBug? Do you see a call to the server or any JS error?
UPDATE
if the problem is to run it on page upload event. You should put the script in the end of your page. Something like described here.