2017-03-07 113 views
-2

在JavaScript的IM初學者,所以我需要一些幫助痘痘:) 我互聯網上找到的引導模式AJAX加載php文件的一些教程,但還是有一些問題..JavaScript的初學者,加載PHP文件

這裏的代碼:

的public_html /資產/ JS/m.js

$(document).ready(function(){ 

    // modals 
    $(document).on('click', '#getUser', function(e){ 

     e.preventDefault(); 

     var uid = $(this).data('id'); // it will get id of clicked row 

     $('#dynamic-content').html(''); // leave it blank before ajax call 
     $('#modal-loader').show();  // load ajax loader 

     $.ajax({ 
      url: 'ajax.php', 
      type: 'POST', 
      data: 'id='+uid, 
      dataType: 'html' 
     }) 
     .done(function(data){ 
      console.log(data); 
      $('#dynamic-content').html('');  
      $('#dynamic-content').html(data); // load response 
      $('#modal-loader').hide();  // hide ajax loader 
     }) 
     .fail(function(){ 
      $('#dynamic-content').html('<i class="glyphicon glyphicon-info-sign"></i> Something went wrong, Please try again...'); 
      $('#modal-loader').hide(); 
     }); 

    }); 
}); 

公共/資產/包括/ pagefile.php

調用模式:

<button data-toggle="modal" data-target="#view-modal" data-id="$cat[id]" id="getUser" class="btn btn-success">Open</button> 

<div id="view-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;"> 
    <div class="modal-dialog"> 
      <div class="modal-content"> 

       <div class="modal-header"> 
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button> 
        <h4 class="modal-title"> 
         <i class="glyphicon glyphicon-user"></i> User Profile 
        </h4> 
       </div> 
       <div class="modal-body"> 

        <div id="modal-loader" style="display: none; text-align: center;"> 
        <img src="ajax-loader.gif"> 
        </div> 

        <!-- content will be load here -->       
        <div id="dynamic-content"></div> 

       </div> 
       <div class="modal-footer"> 
         <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> 
       </div> 

     </div> 
     </div> 

的public_html/ajax.php

<?php 
include("connect_db.php"); 

    if (isset($_REQUEST['id'])) { 
    $id = $_REQUEST['id']; 

    echo "$id"; 
    } else {} 
?>  

一切看起來不錯,但我不能加載Ajax內容,總是模態顯示 出了點錯,請再試...

我想問如果我想在ajax.php文件中添加更多的路徑/任務如何在javascript中添加以及如何發佈/獲取更多的var?

非常感謝,對不起我的英文不好:d

回答

0

您使用的Ajax請求ajax.php相對URL,這意味着從當前頁面相同的路徑(這是public/assets/include/pagefile.php)加載該頁面。
由於ajax.phppagefile.php不在同一條路徑上,因此會出現錯誤。
正確加載ajax.php您可以從Web服務器的根像/ajax.php或相對叫它當前頁../../../ajax.php

$.ajax({ 
     url: '/ajax.php', // or url: '../../../ajax.php', 
     type: 'POST', 
     data: 'id='+uid, 
     dataType: 'html' 
    })