简体   繁体   English

Java脚本和开放图Facebook

[英]java script and open graph facebook

I'm running a javascript application and a part of it runs true like this: 我正在运行一个javascript应用程序,并且其中的一部分运行如下:

document.write("<param name='nick' value='" + nick + "'>\n");

In the <param ....> I cant call the graph api url (this give me a javascript error) so I need to call it before, then take it like a variable and put it in the <param> . <param ....>我无法调用图形api url(这给我带来了JavaScript错误),因此我需要先调用它,然后将其像变量一样放入并将它放在<param> Now I tried a lot of ways to do this without any luck, so I need some help for doing this: 现在,我尝试了很多方法来做到这一点,但没有任何运气,因此我需要一些帮助:

call "the_url/me?fields="firstname" <- this works every time, php, java, html,... 

and now what seems to give me a lot of problems, set the response in a variable or something like this: 现在似乎给我带来了很多问题,将响应设置为变量或类似以下内容:

nick = arg1[]
id = arg2[]
and take it in the document.write() 

document.write("<param name='nick' value='" + nick + "'>\n");
document.write("<param name='id' value='" + id + "'>\n");

Sometimes it sets everything in the first variable; 有时,它将所有内容都设置在第一个变量中。 other times it returns nothing or the wrong thing in the wrong place or .... (you get the point i think). 其他时候,它在错误的地方或没有返回任何错误或错误的东西..(我认为你的意思是)。

Maybe I'm missing something, overlooking a simple way or not using the return right, I'm new to the opengraph thing and maybe I dont have it right .... I don't know anymore so I need help or advice 也许我错过了一些东西,忽略了一种简单的方法,或者没有使用返回权,我对opengraph还是陌生的,也许我没有正确的选择....我不知道了,所以我需要帮助或建议

Update: 更新:

i did use fb.api like this 我确实像这样使用fb.api

    function ShowMyName() {
        FB.api("/me", function (response) {
            document.jform.nick.value = response.name ; 
                });
}

then use the form so people have the option to change there nick before they start, the wierd thing about it is when i call the functoin with a alert it gives me the name but the other part give's me a undifent return, thats why i asked for help, i don't get why the msgbox works on the same script and the other part stays undifent, its the same script 然后使用该表单,以便人们可以选择在开始之前更改昵称,这很奇怪,当我通过警报调用functoin时,它给了我名字,而另一部分给了我一个明确的回报,这就是为什么我问寻求帮助,我不明白为什么msgbox可以在相同的脚本上工作,而另一部分则保持不变,相同的脚本

so this works: 所以这工作:

    <button name="my_full_name" onclick="testbut()" value="My Name" />
<script language="javascript" type="text/javascript">
function testbut() {
        FB.api("/me",
                function (response) {
                    alert('Name is ' + response.name);
                });

    }
</script>

and on the same script this won't work, 并且在相同的脚本上这将不起作用,

    function ShowMyName() {
        FB.api("/me", function (response) {
            document.jform.nick.value = response.name ; 
                });
}

i'm sure the document is right because i can put anythin in the value part and that wil show up .... 我确定文件是正确的,因为我可以将任何东西放在价值部分中,而这会显示出来...。

I hate to give an answer that starts with a documentation link, but it looks like this is your first application. 我讨厌给出一个以文档链接开头的答案,但是看起来这是您的第一个应用程序。 This exact question is answered by the documentation as an example here: https://developers.facebook.com/docs/reference/javascript/ 本文以示例的形式回答了这个确切的问题: https : //developers.facebook.com/docs/reference/javascript/

This call must be made asynchronously as you are waiting for remote data. 在等待远程数据时,必须异步进行此调用。 You then need to execute a callback function upon receipt of data, otherwise your variables will be empty and your code will break. 然后,您需要在接收到数据后执行回调函数,否则变量将为空并且代码将中断。 The OpenGraph JavaScript API works well to do this for you. OpenGraph JavaScript API可以很好地为您完成此任务。

Basically, you need to make the call asynchronously and exec the document.write() calls as part of your callback function. 基本上,您需要异步进行调用,并在回调函数中执行document.write()调用。 So, after you've authenticated and all that, do something like this: 因此,在通过身份验证之后,请执行以下操作:

FB.api('/me', function(response) {
  nick = response.name;
  document.write("<param name='nick' value='" + nick + "'>\n");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM