getting data from cgi into javascript (ajax)

2019-07-20 03:02发布

问题:

I have made a cgi program in C, which generates HTML, and I would like to replace a part of a HTML page with it. So, using jquery I have tried to get the output of my cgi using $.ajax(),$.get() or $.post() but it doesn't work and I found nothing relevant in firefox and chromium's debugger. I run Apache localy, and the logs say it did have a request

127.0.0.1 - - [20/May/2013:01:19:32 +0200] "GET /cgi-bin/test_cgi HTTP/1.1" 200 682

I have looked for hours, and it seems like people are using the code just like I do but it works for them, not for me, so I'll just paste it and let you take a look to what is wrong

javascript(jquery):

$(document).ready(function() {
    $('#button_ajax').click(function() {
        alert("success1");
        $.get('http://localhost/cgi-bin/test_cgi', function(data) {
                    //$('#ajax').empty().append(data);
            alert("success2");
        });
    });
});

html:

<button id="button_ajax">Click here!</button>
<div id="ajax">
<p>Some random test</p>
</div>

There is a pop-up for "success1", but then nothing for "success2" And here is what I get in firefox debugger

> [01:19:32,898] GET http://localhost/cgi-bin/test_cgi [HTTP/1.1 200 OK
> 2ms]

with chromium debugger I get this

XMLHttpRequest cannot load http://localhost/cgi-bin/test_cgi. Origin null is not allowed by Access-Control-Allow-Origin.

Anyone up for some recommandations?

回答1:

I'm able to run the code as you've presented it. I have also alerted the data so I can see that it is indeed retrieving the contents of the page. How does yours differ?

This might indicate that there is some issue with the configuration of your web server.

<html>
   <head>
      <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
      <script type="text/javascript">
     $(document).ready(function() {
        $('#button_ajax').click(function() {
           alert("success1");
           $.get('http://localhost:8888/so/test2.html', function(data) {
               //$('#ajax').empty().append(data);
               alert("success2" + data);
           });
        });
    });
      </script>
   </head>
   <body>
      <button id="button_ajax">Click here!</button>
      <div id="ajax">
         <p>Some random test</p>
      </div>
   </body>
</html>


标签: jquery ajax cgi