php - 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);
?>
解决方案
您使用了错误的变量名:
var toEncode = $('#toEncode').val();
推荐阅读
- javascript - 不止一个验证,令人困惑
- node-red - 如何“连接”动态创建的 UI 开关?
- c - C 函数的内存布局
- php - 如何获得一个类似 array_walk 的 PHP 函数,它会返回一个数组?
- java - JavaFx 桌面应用程序未在 Mac OS Mojave 版本 10.14 上启动
- mongodb - MongoDB Stitch - 身份验证和颁发令牌的服务
- xml - 使用显示多个键的 xmllint 解析 XML
- javascript - Chrome XSL 转换插入脚本节点安全错误?
- javascript - 如何在 ReactJS 中没有任何给定事件的情况下更新 UI
- c# - 我应该在 ViewModel 还是 BL 中处理异常?