繁体   English   中英

如何使用 Ajax 和 JSON 从 PHP 文件访问变量? (数据未定义错误)

[英]How do I access variables from PHP file using Ajax and JSON? (data is not defined error)

我有这个 PHP 文件:

JSONtest.php

<?php
$a=5;
echo json_encode($a);
//This converts PHP variable to JSON.
?>

我想使用 Ajax 和 JSON 来提醒这个变量的值,为此我编写了这个脚本:

学习JSON.php

$(document).ready(function(){
    $("button").click(function(){
    $.ajax({
        url: 'JSONtest.php',
        type: 'POST',
        data: data,
        dataType: 'json',
        success: function(result){
            alert(result);
            },
        error: function(){
            alert("Error");
            }
        });
    });
});

但是当我单击按钮时,我收到此错误消息: learningJSON.php:14 Uncaught ReferenceError: data is not defined

我在做什么错? 我怎样才能解决这个问题?

<?php
$a=5;
echo json_encode($a);
//This converts PHP variable to JSON.
?>

不,它没有。 将简单数字转换为 JSON 有什么意义? 它保持数字5

现在真正的问题。 是的,您的data变量未在您的 JavaScript 代码中的任何地方定义。 如果您没有要发送的数据,请删除该参数。

但是,如果您仍然想传递一些数据,请相应地定义它。 例如

data: { fname: "John", lname: "Doe" }

现在让我们说在你的下一个练习中你想要发布表单数据,你可以使用这个名为serialize() 的好函数。 这将从您的表单中获取所有可发布的字段,并将它们与此请求一起发送。

data : $("#formID").serialize()

数据变量未定义,可以删除

php文件

<?php
$a = $_REQUEST['number'];
echo json_encode($a);
//This converts PHP variable to JSON.
?>

Javascript文件

$(document).ready(function(){
    $("button").click(function(){
    $.ajax({
        url: 'JSONtest.php',
        type: 'POST',
        //data: {'number' : 10}, //this is when you need send parameters to the call, uncomment to send it parameters
        dataType: 'json',
        success: function(result){
            alert(result);
            },
        error: function(){
            alert("Error");
            }
        });
    });
});

我觉得这个应该很适合你。 我们需要3个文件

  1. 索引.php
  2. 登录.js
  3. 登录.php

这意味着当用户提交 [ index.php ] 脚本 js 文件 [ login.js ] 在login.js运行 ajax 进程脚本 [ json ] 时,通过从表单输入 [index.php] 收集所有数据并发送和运行脚本login.php 。 ..这是ajax & json的强大脚本

检查下面的代码

索引.php

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="login.js" type="text/javascript" charset="utf-8">  </script>
</head>
    <body>      

        <form method="post" id="login" name="login">
            Email: <input type="email" id="log-mail" name="log-mail"  > <br />
            Password: <input type="password" id="log-pass" name="log-pass" > <br />
            <button type="submit" >Log in</button>
        </form>

    </body>
</html>

登录.js

$(document).ready(function(){

    // #login = login is the name of our login form
    $('#login').submit(function(e) {
        $.ajax({
            type: "POST",
            url: "login.php",
            data: $('#login').serialize(),
            dataType: "json",
            success: function(msg){

                if(parseInt(msg.status)==1)
                {
                    window.location=msg.txt;
                }
                else if(parseInt(msg.status)==0)
                {
                    window.location=msg.txt;
                }                               
            }
        });
        e.preventDefault();

    });


});

登录.php

<?php


    if(isset($_POST['log-mail']) && $_POST['log-mail'] != ''  && isset($_POST['log-pass']) && $_POST['log-pass'] != '' ) {  
        $_data_ = 'index.php?user_data='.$_POST['log-mail'];
        echo msg_result(1,$_data_);
    }else{
        $msg_att = "index.php?login_attempt=1";
        echo msg_result(0,$msg_att);   

    }



    function msg_result($status,$txt) {
        return '{"status":'.$status.',"txt":"'.$txt.'"}';
    }


?>

你可以在你的网址上看到,如果你

  1. 完成所有字段 => ...index.php?user_data=user_data@gmail.com
  2. 未完成 => ...index.php?login_attempt=1

希望这能解决您的问题

暂无
暂无

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

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