dongmu2026 2014-03-27 15:35 采纳率: 100%
浏览 46
已采纳

ajax运行php文件

I have this script in a .html file on my page and im trying to just run the getip.php file from here rather than to rename the file to .php. I tried this ajax call but its not working and not logging the ip in the logfile.txt. Please let me know what I am doing wrong.

Ajax in the head of the .html file:

 <script>
    $(document).ready(function() {
         $.ajax({

                type: "GET",
                url: "getip.php",

            }); // Ajax Call
        }); //event handler
   </script>

Code from getip.php:

<?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."
");

fclose($fp);
?>
  • 写回答

2条回答 默认 最新

  • drfb52000 2014-03-27 15:39
    关注

    Have you tried $.post() function instead? Example:

    <script>
        $(document).ready(function() {
             $.post('getip.php'); // Ajax Call
            }); //event handler
    </script>
    

    You can also add a callback as a second parameter.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?