2017-03-07 45 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' 
    })