我用C编写了一个cgi程序,它生成HTML,我想用它替换HTML页面的一部分。 因此,使用 jquery 我尝试使用 $.ajax()、$.get() 或 $.post() 获取我的 cgi 的输出,但它不起作用,而且我在 firefox 和 chromium 的调试器中没有发现任何相关内容。我在本地运行 Apache,日志显示它确实有一个请求
127.0.0.1 - - [20/May/2013:01:19:32 +0200] "GET /cgi-bin/test_cgi HTTP/1.1" 200 682
我已经找了好几个小时了,似乎人们正在像我一样使用代码,但它对他们有效,对我不起作用,所以我只是粘贴它,让你看看哪里出了问题
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 调试器中得到的内容
> [01:19:32,898] GET http://localhost/cgi-bin/test_cgi [HTTP/1.1 200 OK
> 2ms]
使用 chromium 调试器我得到了这个
XMLHttpRequest cannot load http://localhost/cgi-bin/test_cgi. Origin null is not allowed by Access-Control-Allow-Origin.
有人需要一些推荐吗?
最佳答案
我能够运行您提供的代码。我还提醒了数据,因此我可以看到它确实正在检索页面的内容。你的有何不同?
这可能表明您的网络服务器的配置存在一些问题。
<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 - 从 cgi 获取数据到 javascript (ajax),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16640626/