Невозможно вызвать функцию AJAX в PHP - PullRequest
0 голосов
/ 20 ноября 2019

Я хочу вызвать ajax из php (я разместил функцию и php на одной странице), но я получаю сообщение об ошибке неопределенного хэша индекса. Пожалуйста, помогите мне, ребята. это мой код:

<html>
<head>
<script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256- 
FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
</head>
 <body>

 </body>
 </html>
 <?php
    echo $_POST["hash"];
 ?>
<script type="text/javascript">
    var hash = window.location.hash;
    $.ajax({
            url:"test.php",
            method:"POST",
            data:{hash:hash},
            success: function (data)
            {
            }
        });
</script>

Ответы [ 2 ]

0 голосов
/ 20 ноября 2019

Вот пример test.php

<html>
<head>
<script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256- 
FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
</head>
<body>

<script type="text/javascript">
var hash = window.location.href;
$.ajax({
        url:"test1.php",
        method:"POST",
        data:{hash:hash},
        success: function (data)
        {
             alert(data);
        }
    });
</script>
</body>
</html>

Вот test1.php

<?php    echo $_POST["hash"];  ?>
0 голосов
/ 20 ноября 2019

Вам нужно иметь 2 файла, скажем index.html:

<html>
<head>
<script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256- 
FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
</head>
 <body>

 </body>
 </html>
<script type="text/javascript">
    var hash = window.location.hash;
    $.ajax({
            url:"test.php",
            method:"POST",
            data:{hash:hash},
            success: function (data)
            {
            }
        });
</script>

и test.php:

<?php
    echo $_POST["hash"];
 ?>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...