无法使用ajax将数据发送到php文件

问题描述 投票:0回答:1

我有这个php文件graph.php

$host = $_POST['hostname'];
echo $type=$_POST['type_char'];
include('rrdtools.inc.php');
include('graphs/'.$type.'.inc.php');

我尝试使用此ajax代码将数据发送到此文件

var type_char='fortigate_cpu';//$('#graph').val();
var hostname='10.10.0.144';//$(this).attr('id');
//$('#device_host').val(id);

$.ajax({
    type: 'POST',
    url: 'SNMP/graph.php',
    data: { hostname:hostname,type_char:type_char },
    success: function(data) {
        alert(data);
        // show the response
        $("#grph").attr("src", 'SNMP/graph.php');
        console.log(data);
    }
});

我将数据发送到该文件时的结果是

当我在apache日志中打开error.log文件时,fortigate_cpu作为type_char变量的值我有这条消息

include():无法打开'graphs / .inc.php'以包含(include_path ='。:/ usr / share / php')

正如你看到fortigate的值不包含在include函数中,即使char_type变量是由ajax发送并打印在页面include文件中必须是这样的

include( 'graphs/fortigate_cpu.inc.php')  

为什么类型不包含在include会话中,即使从ajax接收变量也是如此

javascript php ajax
1个回答
1
投票

正如评论中其他用户所提到的,也许你的问题是你在包含rrdtools.inc.php之后将类型设置为不同的值。

尝试随机化(更改名称),类型变量:

$host = $_POST['hostname'];
echo $type123456=$_POST['type_char'];
include('rrdtools.inc.php');
include('graphs/'.$type123456.'.inc.php');

这是我唯一能想到的,因为我(和其他人)都测试了你的代码。 (前端和后端)。

PS:包括使用post param是一种不好的做法。

© www.soinside.com 2019 - 2024. All rights reserved.