首页 > 解决方案 > PHP、AJAX、JSON - 两个页面之间的数据交换

问题描述

我有两个网页,我正在使用 javascript (ajax) 来调用一个 php 页面。我想将 json 文件从 change.php 接收到 client.html 中的脚本以打印结果。问题是:我不知道如何将 json 文件发送到 client.html 中的脚本以打印结果
这里是页面:

客户端.html

<form>
 <input type="text" id="toEncode" name="toEncode"/>
 <input type="submit" onclick="changeFunction()"/>
</form>

<script src="jquery-3.3.1.min.js"></script> 
<script type='text/javascript'>

function changeFunction(){
    var userName = $('#toEncode').val();
    $.ajax({
        method: "POST",
        url: "change.php",
        data: { "msg": toEncode},
        success: function (response){
            window.alert("Done!");
           //decode json and print the result
        }
    });
}

更改.php

<?php
 $temp= $_POST["msg"];
 $temp2= $temp.' ,hi';
 json_encode($temp2);
?>

标签: phpjsonajax

解决方案


您使用了错误的变量名:

var toEncode = $('#toEncode').val();


推荐阅读