2017-05-16 36 views
0

我需要我的代碼幫助,因爲我一直在瀏覽互聯網尋找我的問題的答案,但仍然可以得到答案,可以解決我的問題。我有點新的使用AJAX。我想從php文件中的json_encode顯示數據到我的AJAX,以便AJAX可以將它傳遞給HTML中的文本框。沒有數據從jquery接收json_encode

我的問題是php文件中的Json_encode有來自json格式查詢的數據,但是當我將它傳遞給ajax成功時,函數(用戶)是空的。 Console.log也是空的數組。我曾嘗試使用JSON.parse,但仍然在代碼中出現錯誤,因爲用戶本身是空的。請任何幫助,將不勝感激。謝謝。

car_detail.js

$(document).ready(function() { 


     function $_GET(q,s) { 
      s = (s) ? s : window.location.search; 
      var re = new RegExp('&'+q+'=([^&]*)','i'); 
      return (s=s.replace(/^\?/,'&').match(re)) ?s=s[1] :s=''; 
     } 
      var car_rent_id1 = $_GET('car_rent_id'); 
      car_rent_id.value = car_rent_id1; 

        $.ajax({ 
     type: 'POST', 
     url: "http://localhost/ProjekCordova/mobile_Rentacar/www/php/car_detail.php", 
     dataType: "json", 
     cache: false, 
     data: { car_rent_id: this.car_rent_id1 }, 
     success: function(users) { 

     console.log(users); 
     $('#car_name').val(users.car_name); 

     } 
    }); 
    }); 

car_detail.php

$car_rent_id = $_GET['car_rent_id']; 
    $query = mysql_query("SELECT c.car_name, c.car_type, c.car_colour, 
    c.plate_no, c.rate_car_hour, c.rate_car_day, c.car_status, 
    r.pickup_location 
    FROM car_rent c 
    JOIN rental r ON c.car_rent_id=r.car_rent_id 
    WHERE c.car_rent_id = $car_rent_id"); 

    $users = array(); 
     while($r = mysql_fetch_array($query)){ 
    $user = array(
     "car_name" => $r['car_name'], 
     "car_type" => $r['car_type'], 
     "car_colour" => $r['car_colour'], 
     "plate_no" => $r['plate_no'], 
     "rate_car_hour" => $r['rate_car_hour'], 
     "rate_car_day" => $r['rate_car_day'], 
     "car_status" => $r['car_status'], 
     "pickup_location" => $r['pickup_location'] 
     ); 
     $users[] = $user; 
    // print_r($r);die; 
    } 
    print_r(json_encode($users)); //[{"car_name":"Saga","car_type":"Proton","car_colour":"Merah","plate_no":"WA2920C","rate_car_hour":"8","rate_car_day":"0","car_status":"","pickup_location":""}] 

car_detail.html

<label>ID:</label> 
       <input type="text" name="car_rent_id" id="car_rent_id"><br> 

       <label>Car Name:</label> 
       <div class = "input-group input-group-sm"> 
        <span class = "input-group-addon" id="sizing-addon3"></span> 
        <input type = "text" name="car_name" id="car_name" class = "form-control" placeholder = "Car Name" aria-describedby = "sizing-addon3"> 
       </div></br> 

       <label>Car Type:</label> 
       <div class = "input-group input-group-sm"> 
        <span class = "input-group-addon" id="sizing-addon3"></span> 
        <input type = "text" name="car_type" id="car_type" class = "form-control" placeholder = "Car Type" aria-describedby = "sizing-addon3"> 
       </div></br> 
+0

複製此林的簡寫e'data:{car_rent_id:this.car_rent_id1},'不會幫助! – RiggsFolly

+2

另外,你已經有了');'你的'success()'方法看起來不像應該在那裏。 –

+0

您忘記關閉'$ .ajax()',您的代碼中充滿了語法錯誤 – julekgwa

回答

2

刪除this這一點也適用HEADGET,在你的AJAX您使用POST,但在你的PHP您使用$_GET。並且car_rent_id未定義,您的功能$_GET(q,s)需要兩個參數,並且只傳遞一個參數。

$(document).ready(function() { 
    function $_GET(q,s) { 
     s = (s) ? s : window.location.search; 
     var re = new RegExp('&amp;'+q+'=([^&amp;]*)','i'); 
     return (s=s.replace(/^\?/,'&amp;').match(re)) ?s=s[1] :s=''; 
    } 
    var car_rent_id1 = $_GET('car_rent_id'); // missing parameter 
    car_rent_id.value = car_rent_id1; // where was this declared? 

    $.ajax({ 
     type: 'POST', 
     url: "http://localhost/ProjekCordova/mobile_Rentacar/www/php/car_detail.php", 
     dataType: "json", 
     data: { car_rent_id: car_rent_id1 }, 
     success: function(users) { 
      console.log(users); 
      $('#car_name').val(users.car_name); 
     } 
    }); 
}); 

您還可以使用$.post(),後期只是爲了$.ajax()

$(document).ready(function() { 
    function $_GET(q,s) { 
     s = (s) ? s : window.location.search; 
     var re = new RegExp('&amp;'+q+'=([^&amp;]*)','i'); 
     return (s=s.replace(/^\?/,'&amp;').match(re)) ?s=s[1] :s=''; 
    } 
    var car_rent_id1 = $_GET('car_rent_id'); 
    car_rent_id.value = car_rent_id1; 

    $.post('http://localhost/ProjekCordova/mobile_Rentacar/www/php/car_detail.php', { car_rent_id: car_rent_id1 }, function (users) { 
     console.log(users); 
     $('#car_name').val(users.car_name); 
    }); 
}); 

,並在您PHP變化

$car_rent_id = $_GET['car_rent_id']; 

$car_rent_id = $_POST['car_rent_id']; 
+0

thankssss !!!我已根據您的建議進行更改,現在可以使用。 – Luqman305

+0

@ Luqman305我很高興能幫上忙。 – julekgwa

0

下面是使用.done/.fail /。總是

一個代碼骨架
<script 
    src="https://code.jquery.com/jquery-1.12.4.min.js" 
    integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" 
    crossorigin="anonymous"></script> 

<script> 
$(function(){ 
    $.ajax({ 
    url: 'theurl', 
    dataType: 'json', 
    cache: false 
    }).done(function(data){ 
    console.log(data); 
    }).fail(function(data){ 
    console.log(data); 
    }).always(function(data){ 
    console.log(data); 
    }); 
}); 
</script> 

我已經適應你的代碼,這樣你就可以看到錯誤,對這個這個

<script> 
    $.ajax({ 
     url: "theurl", 
     dataType: "json", 
     data: { car_rent_id: car_rent_id1 }, 
     success: function(users) { 
     console.log(users); 
     $('#car_name').val(users.car_name); 
     }, 
     error: function(data) { 
     console.log(data); 
     alert("I failed, even though the server is giving a 200 response header, I can't read your json."); 
     } 
    }); 
</script> 
+0

'$ .ajax({})。success(function(){...});'和'$ .ajax({success:fun ...})有區別' – julekgwa

+0

謝謝。我對此很清楚。我向他提供了不建議使用的代碼骨架。 – pendo

+0

他正在使用的代碼未被棄用。 – julekgwa

0

一對夫婦的建議更換Ajax調用,我會按照jQuery的API來嘗試一下看看那裏的請求正在失敗http://api.jquery.com/jquery.ajax/。另外,我會用jQuery訪問輸入文件的id。例如:$("#theID").val()。在this.car_rent_id1cache: false