简体   繁体   English

解析 AJAX 和 php 文件之间的 JSON 数据

[英]Parsing JSON data between AJAX and php file

So I am running into an issue with my project.所以我的项目遇到了问题。 I have two files im running which are admin.php and register.php .我有两个文件正在运行,它们是admin.phpregister.php When I run these files locally I get no issues.当我在本地运行这些文件时,我没有遇到任何问题。 Basically I validate the form in my admin file which returns JSON data to the register file.基本上我验证了我的管理文件中的表单,它将 JSON 数据返回到寄存器文件。 The only issue is, when I run my project on a VPS my register page no longer reads exported JSON data from my admin page and I can't figure out why.唯一的问题是,当我在 VPS 上运行我的项目时,我的注册页面不再从我的管理页面读取导出的 JSON 数据,我不知道为什么。

Below are snipets of code |以下是代码片段| Admin.php Admin.php

    $status = array("status"=>2,"msg"=>"Email already exist.");
    return json_encode($status, JSON_FORCE_OBJECT);
    exit;

Register.php寄存器.php

           $.ajax({
                url:'./chat/ajax.php?action=create_account',
                data: new FormData($(this)[0]),
                cache: false,
                contentType: false,
                processData: false,
                method: 'POST',
                type: 'POST',
                error:err=>{
                    console.log(err)
                },
                success:function(resp){
                    resp = JSON.parse(resp)
                    var newdata = data.status;
                    console.log(resp);
                    if(newdata == 1){
                            location.replace('login.php')
                    }else{
                        console.log(resp[status]);
                        $('#msg').html("<div class='alert alert-danger'>Caught Error</div>")
                        end_load()
                    }
                }
            })

You can try echo on admin.php like below code您可以尝试在 admin.php 上回显,如下面的代码

$status = array("status"=>2,"msg"=>"Email already exist.");
echo json_encode($status, JSON_FORCE_OBJECT);
exit;

Maybe it's due to lack of php json extension in the vps that you're using.可能是因为您使用的 vps 中缺少php json 扩展 Install this extension.安装此扩展程序。 You can see https://www.php.net/manual/en/json.installation.php for more info您可以查看https://www.php.net/manual/en/json.installation.php了解更多信息

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

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