必承其重 | 欲带皇冠 2018-08-06 16:25 采纳率: 0%
浏览 22

Self-POST AJAX请求

I am having some issues running an AJAX request to the php class that contains the AJAX request. The AJAX request is called used a button, and works partially.

My AJAX request is held in a header.php file, which is included in my kpi2.php. If I specify the "url" for the AJAX call to a test file (in the same directory) the POST is successful and I can see the output. I was under the impression if I removed the "url" option it would indeed post to the same page, what am I doing wrong here?

function executeRefresh(){
        if (control){
            $(".loader").show();
            $.ajax({
                type: "POST",
                data: { refresh: '1' }, 
                success: function(json) {
                    if(!json.error) location.reload(true);
                    $(".loader").hide();
                }
            });
        }
    }

Output when no url is specified (meaning it should be posted to the same file that is calling the AJAX)

Written from /home/kpi/pages/kpi2.phpArray
(
)

This is the output when I have the option url: "test.php" (which has the exact same output but just in a different file.

Written from /home/kpi/pages/test.phpArray
(
    [0] => refresh
)

EDIT: To obtain the output generated from above, a simple export.

$v1 = print_r(array_keys($_POST),true);
$fp = fopen('../data/output.json', 'w');
fwrite($fp, 'Written from /home/kpi/pages/test.php'.$v1);
fclose($fp);

As for the control variable, it is just a simple listener that is true/false depending if the control key is clicked. It does indeed work and I've never had issues with it before.

As for the location.reload() I tried removing it and now it seems that it isn't even writing the php code now.

  • 写回答

1条回答 默认 最新

  • weixin_33725239 2018-08-06 17:03
    关注

    Originally in the header.php I had a button on the navbar that would call the AJAX function thus having the kpi2.php (which included the header.php) to have something posted to it. Once it was posted it would then call another php class.

    Instead what I did was get rid of the onclick=executeRefresh() and then just listened to the button press specifically in the kpi2.php class. This way I could instantly execute the AJAX request to the other class without the un-needed post request to the class.

     $(function() {
            $(".btnRefresh").click( function()
                {
                    if (control){
                        $(".loader").show();
                        $.ajax({
                            url:"../setup/kpi_quality.php",
                            type: "GET",
                            data: { casp: 'AVANT-CAP-321' }, // name of the post variable ($_POST['id'])
                            success: function(json) {
                                //if(!json.error) location.reload(true);
                                $(".loader").hide();
                            }
                        });
                    }
                }
            );
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。