donglu5041 2019-06-14 10:58
浏览 78

.done成功但显示空数据

Once the quantity dropdown button is selected, the price field would be changed accordingly. However, I keep on getting successful alert, but the data is showing an empty result.

I have tried using success instead of done but the result is the same. I have also removed datatype: json, still no changes.

$('#qty').change(function(){
    var val = $(this).val();
    $.ajax({
          method: "GET",
          url: "someURLHERE", 
          data: { val: val},
          dataType: "html"
        })
          .done(function(data) {
              alert("Success" + data);
                $('#price').html(data);
                $('#price').trigger('change');
          }).fail(function()  {
                alert("Fail");
   })

});

Here is my model:

  public function getPrice($product_id, $quantity){
       $sql = "SELECT price FROM `price_table` where quantity = '".$quantity."' and product_id = '".$product_id."' ";
       $query = $this->db->query($sql)->row_array();
       return $query;
   }

And here is my controller:

public function getPrice(){
    $p_id = $_GET['p_id'];
    $qty = $_GET['qty_ag'];

    $price = $this->users_model->getPrice($prpduct_id, $quantity);
    json_ok($price);
}

Now, the alert box is returning: Success{"status":"OK","data":{"pv_value":"120.00"}}

All I want to get is the value 120.00

  • 写回答

2条回答 默认 最新

  • 普通网友 2019-06-14 13:12
    关注

    You said you removed dataType: "JSON", but what type of data you're returning in the server side (PHP), and also, how you're returning it?

    For example

    If you're expectating JSON type of data, then you can still using this in JavaScript:

    $('#qty').change(function(){
        var val = $(this).val();
    
        $.ajax({
          method: "GET",
          url: "someURLHERE", 
          data: {
            val: val
          },
          dataType: "JSON" // notice I've returned it to JSON
        })
        .done(function(data){
          console.log("Success" + data); // notice I'm using "console.log"
          $('#price').html(data);
          $('#price').trigger('change');
        })
        .fail(function(){
          alert("Fail");
        })
    });
    

    But, in the server side, PHP should return data like this:

    <?php
      header("Content-Type: application/json");
      die(json_encode($yourData));
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集