如何在本地运行cgi文件?

时间:2014-02-01 07:34:44

标签: ruby cgi localhost

我是服务器端编程的新手。

我正在处理的教程有两个文件:'index.html'和'auto.cgi'

我知道如何从本地服务器(通过python的内置服务器或ruby的webrick)运行文件,这就是我正在做的事情。

然而,当我对'cgi'文件发出ajax请求时,我收到以下错误

500 (Internal Server Error)

为什么这么


其他信息

ajax调用看起来像这样(简化):

ajax({
   type: "GET", 
   url: "auto.cgi?to=" + "jason",
   onSuccess: function(html){
       //do stuff
   }
});

和'auto.cgi'文件如下:

#!/usr/bin/ruby

require 'cgi'

# Get the 'q' parameter out of the incoming Query String
cgi = CGI.new
q = cgi.params['to']

# Our limited 'database' contains a few users
# with their username and full name
data = [
    {
        "user" => "bradley",
        "name" => "Bradley S"
    },
    {
        "user" => "jason",
        "name" => "Jason S"
    },
    {
        "user" => "john",
        "name" => "John R"
    },
    {
        "user" => "josh",
        "name" => "Josh K"
    },
    {
        "user" => "julia",
        "name" => "Julia W"
    }
]

# Make sure that we print out the correct HTML header
print "Content-type: text/html\n\n";

# Now we "search" through the data
for row in data
    # Looking for users that match our auto-complete search
    if(row["user"] =~ /#{q[0]}/i || row["name"] =~ /#{q[0]}/i)

        print '<li id="' + row["user"] + '">
            <img src="icons/' + row["user"] + '_icon.jpg"/>
            <div>
                <span id="username">' + row["user"] + '</span>
                <span id="fullname">' + row["name"] + '</span>
            </div>
        </li>'
    end    
end

0 个答案:

没有答案