0

我需要使用 AJAX 发送登录状态,这是我的代码:

<img style="display:none;" src="https://twitter.com/login?redirect_after_login=%2Fimages%2Fspinner.gif" onload="show_login_status('Twitter', true)" onerror="show_login_status('Twitter', false)" />

<div id="fb-root"></div>

<script type="text/javascript">
   function show_login_status(network, status)
   {
    if (status)
    {
    alert("Logged in to " + network);
    }else{
     alert("Not logged in to " + network);
    }
   }
</script>

如何从此代码创建一个字符串,该字符串将 AJAX 用于发送...

$.ajax
        ({
          type: 'POST',
          url: 'save.php',
          cache: false,
          data: {
    social : <SOME STRING>,
  },
    });  
4

3 回答 3

1

您可以使用全局变量

<img style="display:none;" src="https://twitter.com/login?redirect_after_login=%2Fimages%2Fspinner.gif" onload="show_login_status('Twitter', true)" onerror="show_login_status('Twitter', false)" />
<div id="fb-root"></div>
<script type="text/javascript">
    function show_login_status(network, status) {
        if (status) {
            logStatus = "Logged in to " + network;
        } else {
            logStatus = "Not logged in to " + network;
        }
        $.ajax({
            type: 'POST',
            url: 'save.php',
            cache: false,
            data: {
                social: logStatus
            },
        });
    }
</script>
于 2013-05-06T22:25:52.247 回答
0

我不确定您在做什么,但是:您可以像这样将其存储为变量。

<script type="text/javascript">
   var msg = "";
   function show_login_status(network, status)
   {
    if (status)
    {
       msg = "Logged in to " + network;
    }else{
       msg = "Not logged in to " + network;
    }
    alert(msg);
   }
</script>

后来在你的ajax中这样称呼它:

$.ajax
        ({
          type: 'POST',
          url: 'save.php',
          cache: false,
          data: {
    social : msg,
  },
});
于 2013-05-06T22:26:50.640 回答
0

使用.done() 方法监听 ajax 响应

$.ajax ({
      type: 'POST',
      url: 'save.php',
      cache: false,
      data: {
          social : <SOME STRING>,
      },
    }).done(function(response){
        //examine the response and update your status here
        show_login_status(network, status)
    });  

您肯定会想阅读有关此文档的文档,其中包含很多有用的信息。

于 2013-05-06T22:30:32.253 回答