运行php文件的ajax
我的页面上有一个.html文件中的脚本,我试图从这里运行getip.php文件,而不是将文件重命名为.php。我尝试了这个ajax调用,但它不起作用,并且没有将ip记录到logfile.txt中。请让我知道我做错了什么 Ajax位于.html文件的头部:运行php文件的ajax,php,ajax,Php,Ajax,我的页面上有一个.html文件中的脚本,我试图从这里运行getip.php文件,而不是将文件重命名为.php。我尝试了这个ajax调用,但它不起作用,并且没有将ip记录到logfile.txt中。请让我知道我做错了什么 Ajax位于.html文件的头部: <script> $(document).ready(function() { $.ajax({ type: "GET", url: "g
<script>
$(document).ready(function() {
$.ajax({
type: "GET",
url: "getip.php",
}); // Ajax Call
}); //event handler
</script>
<?php
// location of the text file that will log all the ip adresses
$file = 'logfile.txt';
// ip address of the visitor
$ipadress = $_SERVER['REMOTE_ADDR'];
// date of the visit that will be formated this way: 29/May/2011 12:20:03
$date = date('d/F/Y h:i:s');
// name of the page that was visited
$webpage = $_SERVER['SCRIPT_NAME'];
// visitor's browser information
$browser = $_SERVER['HTTP_USER_AGENT'];
// Opening the text file and writing the visitor's data
$fp = fopen($file, 'a');
fwrite($fp, $ipadress.' - ['.$date.'] '.$webpage.' '.$browser."\r\n");
fclose($fp);
?>
$(文档).ready(函数(){
$.ajax({
键入:“获取”,
url:“getip.php”,
});//Ajax调用
}); //事件处理程序
来自getip.php的代码:
<script>
$(document).ready(function() {
$.ajax({
type: "GET",
url: "getip.php",
}); // Ajax Call
}); //event handler
</script>
<?php
// location of the text file that will log all the ip adresses
$file = 'logfile.txt';
// ip address of the visitor
$ipadress = $_SERVER['REMOTE_ADDR'];
// date of the visit that will be formated this way: 29/May/2011 12:20:03
$date = date('d/F/Y h:i:s');
// name of the page that was visited
$webpage = $_SERVER['SCRIPT_NAME'];
// visitor's browser information
$browser = $_SERVER['HTTP_USER_AGENT'];
// Opening the text file and writing the visitor's data
$fp = fopen($file, 'a');
fwrite($fp, $ipadress.' - ['.$date.'] '.$webpage.' '.$browser."\r\n");
fclose($fp);
?>
您是否尝试了
$.post()
函数?例如:
<script>
$(document).ready(function() {
$.post('getip.php'); // Ajax Call
}); //event handler
</script>
$(文档).ready(函数(){
$.post('getip.php');//Ajax调用
}); //事件处理程序
您还可以添加回调作为第二个参数。我将使用ajax调用的完整URL。根据您的框架,您可能没有到达预期的端点。您可以尝试
echo'test'在php文件中添加code>,并向ajax调用添加一个done处理程序,以确保首先命中php文件:
.done(function( data ) {
alert(data);
});
响应处理程序.done(function(data))?
另外,您不需要类型,也可以这样做:$.ajax(“getip.php”).done(function(){alert(“done”);}.fail(function(){alert(“failed”);})当您直接访问php文件时,它能工作吗?我将首先测试它,然后一旦我确定它工作正常,就使用Firebug(firefox扩展)来查看ajax正在做什么。在ajax调用中还应该有一个.done和.fail函数,这样您就可以知道是什么了happening@Cookie_J5PHP文件确实有效。你能安装Firebug并查看它不起作用的地方吗?Firebug会告诉你它是否至少调用了该文件以及它发送给你的参数。。此外,如果有任何错误,如500或400,它会让你知道再次尝试你的代码,这实际上是工作。我在ajax调用中留下了一些不起作用的东西。谢谢你的帮助