2014-12-13 71 views
0

我有一個jQuery load()函數,它調用控制器函數,而是從控制器索引函數加載load->view(),導致整個頁面被加載到div中。從控制器加載函數加載控制器的索引函數

HTML 「mypage.php」

<h1 class="output"></h1> 
<button class="button" onclick="accept(); return false;"> Accept</button> 

JS:

function accept(){ 

    $('.output').load('mycontroller/accept'); 

} 

PHP控制器:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Mycontroller extends CI_Controller { 



    function index() { 

     $this->home(); 
    } 


    function home() { 

     $this->load->view('user/home'); 
    } 


/// load page where button is 
    function mypage(){ 

      $this->load->view("user/mypage"); 



} 

function accept(){ 

     echo 'HELLO PHP controller'; 

    } 


} 

如果我從控制器刪除索引功能,它不發生,但仍然從控制器的「接受」功能不運行:(

我有一個類似的頁面「home」,它在索引函數中被調用。因此http://localhost:8888/mysite/mycontroller/它具有相同類型的load(),它的工作原理。但是,如果在'/ mycontroller'之後追加/indexhome,則與其他頁面「mypage」有同樣的問題。所以就像load()函數只有在指向沒有任何功能的控制器本身時才起作用。請問這與.htaccess 設置有關嗎?

的.htaccess:

Options -Indexes 

Options +FollowSymLinks 

DirectoryIndex index.php 

<IfModule mod_rewrite.c> 
    RewriteEngine on 
    RewriteCond %{REQUEST_FILENAME} !index.php 
    RewriteRule (.*)\.php$ index.php/$1 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(.*)$ index.php/$1 [QSA,L] 

</IfModule> 

會明白任何幫助。 在此先感謝。

+0

,代碼可以幫助其他地區 - PHP至少 – Deryck 2014-12-13 01:46:18

+0

@Deryck剛剛編輯和添加加載其中按鈕是認爲缺少的功能。就像這樣,仍然索引正在加載而不是接受。這很奇怪,我真的不明白爲什麼不工作。一切似乎都適合我。 – 2014-12-13 02:00:19

+0

'user/home'有一個類似的腳本,工作正常,但我注意到了一些事情:URL是:'http:// localhost:8888/mysite/mycontroller /'它工作正常。但是如果我在控制器後面添加'/ index',頁面加載正常,但Jquery'load()'函數與我在mypage上的最初問題相同。所以我想這是一個路由重定向問題,它可能是.'.htaccess'嗎? – 2014-12-13 19:29:42

回答

1

請使用爲CodeIgniter量身定製的.htaccess,重寫基數很重要我猜你需要使用RewriteBase /mysite。我從一年/兩年前得到它 不記得來源(將搜索它) ,source,另一個是在gist

<IfModule mod_rewrite.c> 
# mod_rewrite rules 
RewriteEngine on 

# The RewriteBase of the system (if you are using this sytem in a sub-folder). 
RewriteBase/

# This will make the site only accessible without the "www." 
# (which will keep the subdomain-sensive config file happy) 
# If you want the site to be accessed WITH the "www." 
# comment-out the following two lines. 
#RewriteCond %{HTTP_HOST} !^localhost$ [NC] 
#RewriteRule ^(.*)$ http://localhost/$1 [L,R=301] 

# If a controler can't be found - then issue a 404 error from PHP 
# Error messages (via the "error" plugin) 
# ErrorDocument 403 /index.php/403/ 
# ErrorDocument 404 /index.php/404/ 
# ErrorDocument 500 /index.php/500/ 

# Deny any people (or bots) from the following sites: (to stop spam comments) 
# RewriteCond %{HTTP_REFERER} nienschanz\.ru [NC,OR] 
# RewriteCond %{HTTP_REFERER} porn\.com 
# RewriteRule .* - [F] 
# Note: if you are having trouble from a certain URL just 
# add it above to forbiden all visitors from that site. 

# You can also uncomment this if you know the IP: 
# Deny from 192.168.1.1 

# If the file is NOT the index.php file 
RewriteCond %{REQUEST_FILENAME} !index.php 
# Hide all PHP files so none can be accessed by HTTP 
RewriteRule (.*)\.php$ index.php/$1 

# If the file/dir is NOT real go to index 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?/$1 [QSA,L] 

</IfModule> 

# If Mod_ewrite is NOT installed go to index.php 
<IfModule !mod_rewrite.c> 
ErrorDocument 404 index.php 
</IfModule> 

#<ifModule mod_expires.c> 
# <filesmatch "\.(ico|flv|jpg|jpeg|png|gif|js|css|swf)$"> 
#  ExpiresActive on 
#  ExpiresDefault "access plus 1 year" 
# </filesmatch> 
#</ifModule> 

<IfModule mod_deflate.c> 
AddOutputFilterByType DEFLATE text/xhtml text/html text/plain text/xml 
text/javascript application/x-javascript text/css 
</IfModule> 

的JavaScript

定義base_url JavaScript的範圍,所以你可以使用JavaScript文件base_url

要麼成爲幫手,要麼在頁面頂部的每個負載的每頁中分配base_url

<script> 
    var base_url = <?= base_url()?> 
</script> 

將代碼放在總是加載的某種視圖中(在執行任何其他JavaScript代碼之前)。

更改您的JavaScript代碼來這一個

function accept() { 
    $('.output').load(base_url + '/mycontroller/accept'); //fix leading slash by inspecting 
} 
+1

將'base_url'傳遞給了javascript;)非常感謝。雖然你發佈的htaccess給我一個內部服務器錯誤。我仍然在使用我正在使用的相同內容。除非我可能在未來遇到麻煩。 – 2014-12-14 00:28:41

相關問題