2015-05-12 115 views
1

我是新來的ajax,我只是嘗試了一些Ajax的例子,但我不斷收到這500內部服務器錯誤。我正在通過網絡尋找解決方案,但似乎沒有任何工作。但是,如果我將POST類型更改爲GET,它工作正常。Ajax POST給500內部服務器錯誤

控制器:

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

class testingAjax extends CI_Controller 
{ 
    public function index() 
    { 
     $this->load->view('testingAjax_view'); 
    } 

    public function getSomething() 
    { 
     $values = $this->input->post('testing');   
    echo $values; 
    } 
} 

js腳本

$(document).ready(
    function() 
    { 
     $('#callBackBtn').click(function() 
     { 
      jQuery.ajax({ 
       type: "POST", 
       url: "testingAjax/getSomething", 
       data: {testing: "testing"}, 
       success: function(data) { 
        $('#responseText').val(data); 
       }, 
       error: function(xhr, text_status, error_thrown){ 
        alert(error_thrown); 
       } 

      })    
     }); 
    } 
); 

視圖

<body> 
    <h3>Testing Ajax</h3> 
    <div class="entry-wrapper"> 
     <input type="text" id="input_text"> 
     <input type="button" value="Ajax Call Back" id="callBackBtn"/> 
    </div> 
    <div class="response_wrapper"> 
     <textarea id="responseText"></textarea> 
    </div> 
</body> 

我上XAMMP運行此。下面是Apache的錯誤日誌(不知道他們有沒有用)

[Wed May 13 00:31:53.251642 2015] [core:notice] [pid 25716:tid 456] AH00094: Command line: 'c:\\xampp\\apache\\bin\\httpd.exe -d C:/xampp/apache' 
[Wed May 13 00:31:53.257646 2015] [mpm_winnt:notice] [pid 25716:tid 456] AH00418: Parent: Created child process 25724 
[Wed May 13 00:31:57.895294 2015] [ssl:warn] [pid 25724:tid 460] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name 
[Wed May 13 00:31:59.065692 2015] [ssl:warn] [pid 25724:tid 460] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name 
[Wed May 13 00:31:59.205786 2015] [mpm_winnt:notice] [pid 25724:tid 460] AH00354: Child: Starting 150 worker threads. 

螢火顯示錯誤:

POST http://localhost/ias/testingAjax/getSomething  
500 Internal Server Error 
     30ms 
+0

你沒有任何形式,你的AJAX沒有向服務器發送任何東西。 –

+0

表單不是問題,但問題是至少你必須發送一些東西到服務器,這不是在你的代碼中發生。 –

+0

我做了一些編輯,現在我有一些數據發送到服務器,但它仍然無法正常工作。 – John

回答

1

url:"/testingAjax/getSomething". 

嘗試Ajax調用默認方法GET你沒有發佈任何form或任何價值的服務器,只需按鈕點擊這就是爲什麼它可以工作的Ajax調用!當你從POST改變方法類型GET但首先我想你應該修復url

Method (default: 'GET')

Type: String The HTTP method to use for the

request (e.g. "POST", "GET", "PUT")

多看這裏http://api.jquery.com/jquery.ajax/

+0

plz解釋爲什麼downvoted? –

+0

我認爲這個人是因爲它是一個控制器類函數,你想通過這個URL調用這是不可能的,所以這種方式 –

+0

Im使用螢火蟲調試,它似乎是正確的網址。 – John

1

我創造了你的工作的一個新的副本,在這裏一個簡單的你可以跟隨。

首先是正確設置您的項目。

首先,我創建一個.htaccess文件的文件夾同一目錄的根文件夾到的index.php以創建一個漂亮的URL

下面是一個簡單的.htaccess文件內容

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule .* index.php/$0 [PT,L] 
</IfModule> 

而且你需要讓服務器的mod_rewrite的延伸/配置

你可以找到appache/httpd.conf中

啓用它,並找到個e「LoadModule rewrite_module modules/mod_rewrite.so」

刪除#並保存並重新啓動appache。

下一步是使URL輔助

應用/配置/自動加載。php

$autoload['helper'] = array('url'); 

使用由CI創建的url助手;

參見URL HELPER

接着是設置默認的控制器執行此 應用/配置/ routes.php文件

$route['default_controller'] = 'testingAjax'; 

控制器testingAjax.php

<?php 

class testingAjax extends CI_Controller 
{ 
    public function index() 
    { 
     $this->load->view('testingAjax_view'); 
    } 

    public function getSomething() 
    { 
     $values = $this->input->post('testing');   
     echo $values; 
    } 
} 

視圖testingAjax_view。 php

<html> 
    <head> 
     <script type="text/javascript"> 
      // get the base url of the project 
      var BASE_URL = "<?php echo base_url(); ?>"; 
     </script> 
    </head> 
<body> 
    <h3>Testing Ajax</h3> 

    <div class="entry-wrapper"> 
     <input type="text" id="input_text"> 
     <input type="button" value="Ajax Call Back" id="callBackBtn"/> 
    </div> 

    <div class="response_wrapper"> 
     <textarea id="responseText"></textarea> 
    </div> 

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> 

    <script type="text/javascript"> 

     $(document).ready(function() { 

      $('#callBackBtn').on('click', function(){ 
       $.ajax({ 
        type: "POST", 
        url: BASE_URL + "testingAjax/getSomething", 
        data: {testing: $('#input_text').val()}, 
        success: function(data) { 
         $('#responseText').val(data); 
        }, 
        error: function(xhr, text_status, error_thrown){ 
         alert(error_thrown); 
        } 

       }); 

       // But if you cannot setup the pretty url 
       // using .htaccess 
       // then you can use the 
       // BASE_URL+ "index.php/" + "testingAjax/getSomething 
      }); 
     }); 

    </script> 

</body> 

</html> 
0

只是把這個在你的.htaccess文件是在根目錄下:

Options +FollowSymLinks 
RewriteEngine on 
RewriteBase/
RewriteCond $1 !^(index\.php|resources|robots\.txt) 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php/$1 [L,QSA] 

注:

RewriteBase /這裏重要的。

相關問題