Ajax POST并将数据作为json返回

时间:2013-11-05 08:28:10

标签: php jquery ajax json

您好,我是Ajax的新手,我想制作一些Ajax post,它将根据UI上的用户输入从mySQL数据库中选择数据并将数据显示为JSON对象,这是我的代码。不工作:

的index.html `                

           </fieldset>
           <form id="search">
                <fieldset>
                    <legend>Masukkan tanggal keberangkatan:</legend>
                    <input type="text" id="datePickerJadwal"/>
                    <input type="button" id="carijadwal" value="Cari jadwal"> 
                </fieldset> 
           </form>
        </div>

    </div>`

index.js

    var url = 'http://yoga-pratama.com/php/Frontend/getjsonp/cariJadwalTravel.php';
 var datejadwal = $('#search').serialize();

 $(document).on('vclick', '#carijadwal', function () { 
        $.ajax({ 
             type:"POST", 
             url: url,
             data:datejadwal,
             //dataType: "jsonp",
             jsonp: 'jsoncallback',
             async: true,
             beforeSend: function(){
              console.log('Sending Ajax request');
              $.mobile.loading( "show", {
                text: "loading data",
                textVisible: true,
                theme: "z",
                html: ""
              });
             },
             success: function (result) {
                 Jadwalrental.parseJSONPJadwalRental(result);
                 console.log('request success');
                 $.mobile.loading('hide');
             },
             error: function (request, error) {
                 alert(error);
             }
         });  

     });

PHP代码:

 <?php
   header('Content-type: application/json');
    include'../connect.php';

    $datePickerJadwal = $_POST['datePickerJadwal'];
    $arr = array();
    $rs = mysql_query("select j.id_jadwal,j.tujuan,j.tgl_jalan,j.jam,j.harga,s.id_jadwal,s.sisa from jadwal j,sisa_seat s where  j.tgl_jalan='". $datePickerJadwal ."'");

    while($obj = mysql_fetch_object($rs)){
    $arr[] = $obj;

    }
echo $_GET['jsoncallback']. '('. json_encode($arr).');';


?>

我编辑代码并且解析错误问题很明显,但它没有提供任何数据,

var  Jadwalrental = {
 parseJSONPJadwalRental: function (result) {
   //rentalInfo.result = result;
  // $('#listRental').empty();

   $.each(result, function (i, row) {
         console.log(JSON.stringify(row));

     });
  // $('#listRental').listview('refresh');
 }

}

0 个答案:

没有答案