dongtangze6393 2016-10-19 17:48
浏览 42

JQuery和Ajax:如何在变量中存储返回的数据?

I have the following code:

  window.onload = function() {  
      var eMail = "<?php echo $log; ?>"; 
      var aRticleid = "<?php echo $articleid1; ?>"; 
      $.ajax({
              type: "GET",
              url: 'aquireLikes.php?email='+eMail+'&id='+aRticleid+'',
              success: function(data){
              }
          });
      };

This executes a php script. There is three variables in the PHP script that gets set to true if certain conditions are met. I want to store the returned value of these variable in a set of JavaScript variables on my page that call the aquireLikes.php page. How can I do this? It would be something like this but actually works:

  window.onload = function() {  
      var eMail = "<?php echo $log; ?>"; 
      var aRticleid = "<?php echo $articleid1; ?>"; 
      $.ajax({
              type: "GET",
              url: 'aquireLikes.php?email='+eMail+'&id='+aRticleid+'',
              success: function(data){

                var liked = aquirelikes.php.$liked;
              }
          });
      };

I know above is complete nonsense but you get the hint. So how can I store the returned values in javascript variables? Here is my aquireLikes file:

    <?php

    $email = $_GET["email"];
    $articleid1 = $_GET["id"];
    $done = false;

     $thing = mysql_query("SELECT `id` FROM likes WHERE id=(SELECT MAX(id) FROM likes)");
     $lastrow = mysql_fetch_row($thing);
     $lastid = $lastrow[0];

     if($lastid == null || $lastid == '0'){
       $lastid = '1';
     }   

     $state1 = '';

     for ($i=1; $i <= $lastid+1; $i++) { 
        $current = mysql_query("SELECT * FROM likes WHERE id=$i");
        while ($row = mysql_fetch_array($current)) {
           $id1 = $row[0];
           $userid1 = $row[1];
           $state1 = $row[2];
           $articleid1 = $row[3];
       if($done == false){
         if($email == $userid1 && $articleid1 == $id && $state1 == '1'){
          $liked = true;
          $disliked = false;
          $done = true;
          echo "<script>console.log('Liked!');</script>";
          break;
         }else{
          $liked = false;

         }
         if($email == $userid1 && $articleid1 == $id && $state1 == '0'){
          $disliked = false;
          $liked = false;
          $done = true;
          echo "<script>console.log('NONE!');</script>";

          break;
         }
         if($email == $userid1 && $articleid1 == $id && $state1 == '2'){
          $disliked = true;
          $liked = false;
          $done = true;
          echo "<script>console.log('disliked!');</script>";

          break;
         }else{
          $disliked = false;
         }
       }
      }


     }





?>
  • 写回答

1条回答 默认 最新

  • douzhang1926 2016-10-19 17:56
    关注

    Your ajax would look like this:

        var liked;
        $.ajax({
              type: "GET",
              url: 'aquireLikes.php?email='+eMail+'&id='+aRticleid+'',
              type: 'json',
              success: function(data){
    
                liked = data.liked;
              }
      });
    

    And in php you would have to return:

    echo json_encode($data)
    

    Php file has to output only json data!

    This is just example of implementation.

    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决