简体   繁体   English

使用ajax访问模板的php变量

[英]Access php variables of a template with ajax

I know that this question has already been answered in other topics but for some reason it doesn't work for me and I don't understand why.我知道这个问题已经在其他主题中得到了回答,但由于某种原因它对我不起作用,我不明白为什么。

I call a template with ajax and inside it there are set some php variables and i need to get those values.我用 ajax 调用模板,其中设置了一些 php 变量,我需要获取这些值。

first-template.php第一个模板.php

<?php
$advert = array(
        'ajax' => 'Hello world!',
        'advert' => 'Bye',
     );
    echo json_encode($advert);
?>

second-template.php第二个模板.php

?>
<script>
jQuery(document).ready(function($){
          
 $.ajax({
        beforeSend: function(){
        alert('Requesting...');
        },
        url : 'first-template.php',
        type : 'GET',
        dataType : 'json',
        success : function (result) {
           alert(result['ajax']); // "Hello world!" alerted
           alert(result['advert']) // "Bye" alerted
        },
         error : function(xhr, status, error) {
  alert(error);
}
    });

      });


</script>

This is how it is shown here but the function returns error.这是这里显示的方式,但函数返回错误。

First, maybe, you can change the request type to GET, because you don't post anything, but tried to get the information.首先,也许,您可以将请求类型更改为 GET,因为您不发布任何内容,而是尝试获取信息。

<script>
jQuery(document).ready(function($){
          
 $.ajax({
        beforeSend: function(){
        alert('Requesting...');
        },
        url : 'first-template.php',
        type : 'GET',
        dataType : 'json',
        success : function (result) {
           alert(result['ajax']); // "Hello world!" alerted
           alert(result['advert']) // "Bye" alerted
        },
        error : function () {
           alert("error");
        }
    });

      });


</script>

EDIT:编辑:

Here is a functional script:这是一个功能脚本:

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(function() {
                
        $.ajax({
                beforeSend: function(){
                alert('Requesting...');
                },
                url : 'first-template.php',
                type : 'GET',
                dataType : 'json',
                success : function (result) {
                    console.log(result);
                },
                error : function(xhr, status, error) {
        alert(error);
        }
            });

        });
    </script>

See the modifications:查看修改:

  • GET instead of POST GET 而不是 POST
  • console.log(result) to see the result console.log(result) 查看结果

In my browser console:在我的浏览器控制台中:

Object { ajax: "Hello world!", advert: "Bye" }

Just to be sure, do you load your JQuery properly and do you work on a WAMP/LAMP server?可以肯定的是,您是否正确加载了 JQuery 并且您是否在 WAMP/LAMP 服务器上工作?

Can you post the entire files code?您可以发布整个文件代码吗?

EDIT2:编辑2:

Check error in console and post it here, this is more easy to find the problem in the future with it.检查控制台中的错误并将其发布在此处,这更容易在将来找到问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM