duanliusong6395 2012-11-21 01:48
浏览 43
已采纳

Ajax和JSON响应问题

I'm a pretty novice programmer who is making an application where there are multiple divs with a class of jcontainer. Each jcontainer has an id associated with it, which corresponds with a field called apptid in a database. In the Jquery, I send out an AJAX request every five seconds to a php page to retrieve a field called currentlocation in the database (that corresponds with the apptid), which happens for each jcontainer.

Here is my code:

HTML:

<div class='jcontainer' data-param='jcontainer_IDNUMBER'>

  <button class='checkIn' data-param='button_IDNUMBER'>Check In</button>
  <form method='post' class='myForm' action=''>
       <select name='locationSelect' class='locationSelect' data-param='location_IDNUMBER'>
            <option value='1'>Exam Room</option>
            <option value='2'>Exam Room 2</option>
            <option value='3'>X-Ray Room</option>
            <option value='1000'>Check Out</option>
       </select>
  </form>
  <div class='finished' style='color:#ff0000;'>Checked Out</div>

</div>

jQuery:

<script type="text/javascript">
$(document).ready(function() {

  setInterval(function(){

    $('.jcontainer').each(function() {
        var $e = $(this);
        var dataid = $e.data("param").split('_')[1] ;

        $.ajax({
            url: 'heartbeat.php',
            method: 'post',
            contentType: "application/json",
            dataType: "json",
            data: dataid,
            success: function(data){
                var msg = $.parseJSON(response);
                alert(msg);
            }
        });

    });
  },5000);

  //other code that I didn't post, because it's not really relevant, but can post if needed be

and php page:

<?php

$hostname = 'localhost';

$username = '******';

$password = '*************';

$apptid = $_POST['dataid'];

$db = new PDO("mysql:host=$hostname;dbname=sv", $username, $password);
$statement = $db->prepare("SELECT currentlocation FROM schedule WHERE apptid = :apptid");
$statement->execute(array(':apptid' => $apptid));
$row = $statement->fetch();

$currentlocation = array('CurrentLocation' => $row['currentlocation']);
echo json_encode($currentlocation);

?>

The problem is I can't get any response from the alert(msg). Why is this? I checked Chrome Developer and no errors came up.

The end goal is to have the currentlocation number recorded in a jquery variable. ex -> var currentLocation = Number

Thanks for any and all help, and if you need more details to clear things up, I can happily post!

  • 写回答

4条回答 默认 最新

  • dos8410 2012-11-21 01:58
    关注

    This block of code:

    success: function(data){
                var msg = $.parseJSON(response);
                alert(msg);
            }
    

    should read

    success: function(data){
                var msg = $.parseJSON(data);
                alert(msg);
            }
    

    cause the response variable is not declared and undefined so you should use the data variable in your parseJSON method as that is the actual data received. Hope this solves it for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?