

下記のようなJavaScriptのコードでAPIにPOSTのリクエストを送信すると、Internal Server Errorが返ってきてしまいます。

追記：OSはUbuntuでwebサーバーはApache、ブラウザはFirefoxを使っています。

**apitest.html**

[code]

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8" />
      <title>WolftamAlphaAPItest</title>
      <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    </head>
    <body>
      <table id="table" border="1">
        <tr><th>Alternate Form</th></tr>
      </table>
    
      <script>
        var rbFileName = '';
        rbFileName = 'http://xxxxxxxxxxxxxxxxxxxx/wapi.rb';
    
        var results;
    
        var querymml = '<math><mrow%20class="integration-node"><mi%20class="character-node">a</mi><mi%20class="character-node">%2B</mi><mi%20class="character-node">b</mi><mi%20class="character-node">=</mi><mi%20class="character-node">2</mi><msqrt%20class="structure-node"><mrow%20class="integration-node%20highlight"><mi%20class="character-node">a</mi><mi%20class="character-node">b</mi></mrow></msqrt></mrow></math>';
        $.ajax({
                type: 'POST',
                url: rbFileName,
                async: false,
                traditional: true,
                dataType: "json",
                data: {query: querymml},            
                success: function(json) {
                    results = json;
                    console.log(json);
                    showResult( results );
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert(
                        'XMLHttpRequest : ' + XMLHttpRequest.status + '\n' +
                        'textStatus : ' + textStatus + '\n' +
                        'errorThrown : ' + errorThrown.message
                    );
                },
          });
        function showResult( result ) {
    
            $("#table").append("<tr><td>" + result + "</td><td>");
    
        }
      </script>
    </body>
    </html>
    
[/code]

**wapi.rb**

[code]

    #!/usr/bin/ruby
    
    require 'cgi'
    require 'net/http'
    require 'json'
    require 'uri'
    
    cgi = new CGI.new()
    
    mml = cgi['query']
    
    input = "input=#{mml}"
    
    INPUT = "#{input}"
    APPID = 'appid=XXXXXXXXXXXXXXXX'
    INCLUDEPODID = 'includepodid=Alternate form'
    FORMAT = 'format=mathml'
    
    uri = "http://api.wolframalpha.com/v2/query?" + 
            "#{APPID}&#{INPUT}&#{INCLUDEPODID}&#{FORMAT}"
    
    uri = URI.parse(uri)
    req = Net::HTTP::Get.new(uri)
    res = Net::HTTP.start(uri.host, uri.port){|http|
        http.request(req)
    }
    
    data = {result: res}
    
    cgi.out ({ "type" => "application/json", "charset" => "UTF-8" }) {
        data.to_json
    }
    
[/code]

**error.log**

[code]

    [Sun Jul 21 06:25:01.796620 2019] [mpm_prefork:notice] [pid 32227] AH00163: Apache/2.4.18 (Ubuntu) configured -- resuming normal operations
    [Sun Jul 21 06:25:01.796646 2019] [core:notice] [pid 32227] AH00094: Command line: '/usr/sbin/apache2'
    [Sun Jul 21 11:34:32.637240 2019] [cgid:error] [pid 30236] (13)Permission denied: AH01241: exec of '/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/wapi.rb' failed
    [Sun Jul 21 11:34:32.637484 2019] [cgid:error] [pid 29278] [client 180.9.169.100:52500] End of script output before headers: wapi.rb, referer: http://xxxxxxxxxxxxxxxxxxxxxxxxxx/wolframApi.html
    
[/code]


CC BY-SA 4.0
2019-07-19T13:25:38.777
56752
2020-05-29T06:02:40.187
2019-07-21T12:25:20.913
35172
35172
question
javascript ruby
APIのレスポンスを返したいが、Internal Server Errorが返ってしまう


改行コードをCRLFからLFに変更したところ、Internal Server Errorは解決しました。  
これがどうしてか、もっと学習してみようと思います。  
コメントくださった方々、ありがとうございました。


CC BY-SA 4.0
2019-07-24T05:43:34.420
56857
2019-07-24T05:43:34.420
35172
56752
answer