【问题标题】:Ajax POST giving 500 Internal Server ErrorAjax POST 给出 500 内部服务器错误
【发布时间】:2015-05-12 16:53:40
【问题描述】:

我是 ajax 新手,我只是在尝试一些 ajax 示例,但我不断收到 500 Internal Server Error。我在网上搜索解决方案,但似乎没有任何效果。但是,如果我将类型从 POST 更改为 GET,它就可以正常工作。

控制器:

<?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.

Firebug 显示错误:

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

【问题讨论】:

  • 您没有表单,并且您的 AJAX 没有向服务器发送任何内容。
  • 表单不是问题,但问题是至少你必须向服务器发送一些东西,这在你的代码中没有发生。
  • 我进行了一些编辑,现在我有一些数据发送到服务器,但它仍然无法正常工作。
  • 我添加了我的萤火虫显示的错误
  • @John 请为其他人的帮助标记和投票。谢谢。

标签: php jquery ajax codeigniter


【解决方案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 来启用它

并找到“LoadModule rewrite_module modules/mod_rewrite.so”

删除 # 并保存并重新启动您的应用程序。

下一步是启用 URL 助手

应用程序/配置/autoload.php

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

利用 CI 创建的 url helper;

URL HELPER

接下来是设置默认控制器来执行此操作 应用程序/config/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>

【讨论】:

    【解决方案2】:

    试试

    url:"/testingAjax/getSomething".
    

    对于 ajax 调用默认方法是 GET 并且您没有向服务器发布任何 form 或任何值,只是在单击按钮时进行 ajax 调用,这就是它可能起作用的原因!!!当您将方法类型从 POST 更改为 GET 但首先我认为您应该修复 url

    方法(默认:'GET')

    类型:字符串 用于的 HTTP 方法

    请求(例如“POST”、“GET”、“PUT”)

    在此处查看更多信息http://api.jquery.com/jquery.ajax/

    【讨论】:

    • 我认为这个人投了反对票,因为它是你想通过这个 url 调用的控制器类函数,而这种方式根本不可能
    • 我用firebug调试,看来url是正确的。
    【解决方案3】:

    只需将其放在根目录中的 .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 / 在这里很重要。

    【讨论】: