weixin_33726313 2017-08-18 02:05 采纳率: 0%
浏览 12

AJAX调用不适用于PHP

This is making no sense at all.

I've tried to work the code on my web platform and also from my computer.

Here is my checklist.

1) Port 3306 is open for mySQL.

2) Code minimized as shown below.

3) Used an online PHP code tester to see if there are any errors.

4) Used Document.ready and called the method with the ajax call.

5) Used an alert to test if my html contained any errors.

6) Checked the file permissions

7) mysqli

Here is the code:

PHP

<?php
include 'pass.php';
//ini_set('max_execution_time', 4000);
ini_set('memory_limit', '16M');
try {
    $con = mysqli_connect($host, $username, $password, $db_name, $port);

    if (mysqli_connect_error()) {

        echo 'Could not connect to database';

    }
    echo 'hello';
}
catch (Exception $e) {
    echo 'Caught exception: ', $e->getMessage(), "
";
}
mysqli_close($con);
?>

AJAX

function getExample() {
    alert("test");
    $.ajax({
        type: "GET",
        url: "code/getExample.php",
        dataType: "html",
        cache: false,
        success: function(response) {
            alert(response);
        }
    });
}

The alert test is showing on my webpage so no errors so far and the file permissions except for pass is read and write and execute.

From your experience, whats going wrong?

Note: I dont want to send data and eventually I want to retrieve it as json but for the time being Im testing with dataType: 'html',.

I'm expecting hello to appear as an alert but nothing appears only alerts test before the ajax call. ( in reply to comment)

* EDIT *

403 forbidden error... when I set my pass file to read only it resets to read and write... it looks like changing the file permissions does not work.

  • 写回答

2条回答 默认 最新

  • weixin_33717117 2017-08-18 02:29
    关注

    Firstly I suggest you create some simple file to test it. Make sure your JQuery Library included.

    html:

    <button id="testAction">Test Ajax</button>
    

    Jquery:

    $("#testAction").click(function(){
        $.ajax({
            type: "post",
            url: "testAction.php",
            data: {action:"PrintHi", PrintMessage:"HI"},
            success: function(data){
              alert(data);
            }
        });
    });
    

    PHP(testAction.php):

    <?php
        if($_POST["action"]=="PrintHi"){
        echo $_POST["PrintMessage"];
        }
    ?>
    

    If it can work, just start to put your function to your php file.

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置