运行php文件的ajax

运行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

我的页面上有一个.html文件中的脚本,我试图从这里运行getip.php文件,而不是将文件重命名为.php。我尝试了这个ajax调用,但它不起作用,并且没有将ip记录到logfile.txt中。请让我知道我做错了什么

Ajax位于.html文件的头部:

 <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',并向ajax调用添加一个done处理程序,以确保首先命中php文件:

.done(function( data ) {
  alert(data);
});

响应处理程序
.done(function(data))?
另外,您不需要类型,也可以这样做:
$.ajax(“getip.php”).done(function(){alert(“done”);}.fail(function(){alert(“failed”);})