将数据从CGI转换为javascript(ajax) [英] getting data from cgi into javascript (ajax)

查看:128
本文介绍了将数据从CGI转换为javascript(ajax)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C编写了一个cgi程序,该程序生成HTML,我想用它替换HTML页面的一部分. 因此,使用jquery,我尝试使用$ .ajax(),$.get()或$ .post()获取cgi的输出,但是它不起作用,并且在firefox和chromium的调试器中找不到任何相关内容.我在本地运行Apache,日志显示它确实有一个请求

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

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):

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>

"success1"弹出窗口,但"success2"则没有弹出窗口 这就是我在firefox调试器中得到的东西

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.

有人建议吗?

推荐答案

我已经能够按照您介绍的方式运行代码.我还提醒了数据,因此我可以看到它确实是在检索页面的内容.你的有什么不同?

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?

这可能表明您的Web服务器的配置存在问题.

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>

这篇关于将数据从CGI转换为javascript(ajax)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆