json - 将数据发布到 Json 时返回“未定义”错误
问题描述
我想在传递参数 1 时测试我的 ajax 'PUT' 函数,但它返回未定义?
orders.json
[ {“id”:1,“name”:“will_1”,“drink”:“cola_2”},{“id”:2,“name”:“Laura”,“drink”:“香草”
}]
$(document).ready(function(){
$("#purchaseId").change(function() {
$.ajax({
type:'POST',
url:'orders.json',
data: "1",
success:function(newOrder) {
alert(newOrder.name);
},
error:function(){
alert("loading error");
}
});
});
});
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="home.js">
</script>
<title>Javascript on Steroids</title>
</head>
<body>
<select id = "purchaseId" >
<option value="1" selected >1</option>
<option value="2">2</option>
</select>
</body>
</html>
解决方案
当你 POST 或 PUT 时,你发送data: "1",
的只是参数,而不是它的值。在 GET 中它看起来像://someurl/?1=
尝试:data: "order=1",
这将 POST/PUT 一个参数:order
带有值:1
这种方式在后端你可以期待$_POST['order']
, 并将获取它的发送值1
推荐阅读
- security - 关于 package-lock.json 的 Github Pages 安全公告,但该文件不在我的存储库中
- spring-boot - 一对多双向关系实现 postgres SpringBoot
- powerbi - Power BI multiple if 包含从 Tableau 计算度量的拆分转换
- css - 导航删除 Elementor 中的粘性徽标文本 (SVG)
- html - 如何分隔重叠的div块(VueJS)
- python - FFMPEG loudnorm 过滤器不能与消音过滤器结合使用
- python - 错误:连接 ECONNREFUSED 127.0.0.1:5000 或错误:对 docker 容器的 GET 请求后套接字挂断
- typeorm - 使用 TypeORM 的动态 wheres
- graphql - 如何将订阅中的数据附加到集合中?
- node.js - 我可以在不创建实时数据库的情况下部署到 Firebase