Ajax调用什么都不返回

时间:2014-01-04 11:39:38

标签: php jquery

我正在尝试通过点击从$ .each循环动态生成的锚标记进行ajax调用以获取JSON响应。

有关信息:#records是我的表,.update是该表中的锚标记类。

请注意,该表是动态生成的。 现在问题是我的ajax调用什么都没有返回,即使我已经检查了它错误:但没有收到响应。我已经尝试在ajax调用之前警告我的var数据并且它工作。所以问题从ajax调用开始。而且,我的服务器端代码运行正常。

// Update existing customers 
            $("#records").on('click', ".update", function() {
                        var data = '?'+ $(this).attr('id');
                        $.ajax({
                            type: "GET",
                            url: "viewcustomers.php",
                            data: data,
                            success: function(response) {
                                console.log(response);
                            }
                        });
                        });

提前致谢。

以下参考是生成表格的代码。

 // Function to make datagrid
            function getRecords() {
                $.getJSON("viewcustomers.php", function(data) {
                    var items = [];
                    var xTd = '';
                    var xTr = '';
                    $.each(data, function(key, val) {
                        var c = 0;
                        var id = 0;
                        $.each(val, function(key1, val1) {
                            if (c == 0)
                            {
                                id = val1;
                            }
                            c++;
                            xTd += '<td>' + val1 + '</td>';
                        });
                        xTd += '<td><a href="#" id="update=' + id + '" class="update">Edit</a></td>';
                        xTd += '<td><a href="customers.php?delete=' + id + '">Delete</a></td>';
                        xTr = '<tr>' + xTd + '</tr>';
                        items.push(xTr);
                        xTd = '';
                        xTr = '';
                    });
                    $("#records").append(items);
                });
            }

更新了服务器端代码: page url:localhost / hotel / viewcustomers.php

/ **  *获取单行以进行更新/删除。  * /

if(isset($_GET['update'])){
    $customer = new Customers;
    $Id = $_GET['update'];
    $customer_single = $customer->View_Single_Customer($Id);
    echo json_encode($customer_single);
    unset($customer);

}

2 个答案:

答案 0 :(得分:2)

将其修改为

$("#records").on('click', ".update", function() {
  var request = '?id='+ $(this).attr('id');
  $.ajax({
    type: "GET",
    url: "viewcustomers.php" + request,
    success: function(response) {
      console.log(response);
    }
  });
}); 

答案 1 :(得分:2)

此行未正确使用var data = '?'+ $(this).attr('id');

更改为:var my_id = $(this).attr('id');

然后使用data: data

更新行data : {id:my_id}

完整代码:

$("#records").on('click', ".update", function() {
    var my_id = $(this).attr('id');
    $.ajax({
        type: "GET",
        url: "viewcustomers.php",
        data : {id : my_id},
        success: function(response) {
            console.log(response);
        }
    });
});

或者这样做:

$("#records").on('click', ".update", function() {
    var param = '?id='+ $(this).attr('id'); /*notice that I have added "id=" */
    $.ajax({
        type: "GET",
        url: "viewcustomers.php" + param,
        /* remove the data attribute */
        success: function(response) {
            console.log(response);
        }
    });
});
相关问题