Execute script on page load in static FBML
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 开发者_Python百科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>
You should use FBJS (facebook version of JS). You can find documentation about ajax call here
it will be something like
<script><!--
var ajax = new Ajax();
ajax.ondone = function(data) {
//do something
}
ajax.post('http://example.com/testajax.php');
//-->
</script>
UPDATE
try to add next lines just to be sure that this is not a server error and a response type.
req.responseType=Ajax.FBML;
ajax.onerror=function(data){
alert("Something went wrong. Please, try again.");
}
ResponseType can be one of Ajax.RAW, Ajax.JSON, or Ajax.FBML.
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.
精彩评论