使用 jquery 发布 JSON 数据给了我空变量
Posting JSON data using jquery gives me empty variable
我有这个html/javascript:
<html>
<head>
</head>
<body>
<script src="view/backoffice/assets/plugins/jquery/jquery-1.11.1.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
var data = {'user_id':'2680',
'ship_to_name':'John Doe',
'ship_to_address':'Somewhere in Jawa Timur',
'ship_to_city':'Surabaya',
'ship_to_area':'Wonocolo',
'ship_to_phone':'080000000'};
$.ajax({
url: "../controller/ctrl.php",
type: 'POST',
contentType:'application/json',
data: JSON.stringify(data),
dataType:'json'
});
});
</script>
</body>
</html>
我的服务器上有这个 PHP:
<?php
$jsonReceiveData = json_encode($_POST);
file_put_contents('storedvar.php', $jsonReceiveData);
?>
但我storedvar.php
得到的只是 JSON 的空变量。 就像这个[]
.
为什么我无法获取 JSON 发送的数据?谢谢。
您的数据是 json ,而不是字符串。
像这样替换它,但不要拆分行。
var data = "{'user_id':'2680',"+
"'ship_to_name':'John Doe',"+
"'ship_to_address':'Somewhere in Jawa Timur',"+
"'ship_to_city':'Surabaya',"+
"'ship_to_area':'Wonocolo',"+
"'ship_to_phone':'080000000'}";
如果这有效,您可能希望删除字符串并查看原始 JSON 是否有效。
相关文章:
- 如何将变量数据从Java类获取到JavaScript
- 如何获取PHP变量数据并将其存储在JavaScript变量中
- 如何将变量数据存储在其他变量中并对其进行解析
- 如何在没有web服务器的情况下将变量数据从Python传输到Javascript
- 将变量数据添加到 JSON
- 变量数据丢失的技术原因
- 如何从NodeJsServer的查询字符串中获取id或任何变量数据
- 将变量数据从操作脚本 3 发送到 JavaScript
- 将 JS 变量数据发送到可下载页面
- 如何在 php 变量中传递 jquery 变量数据
- 如何将变量数据传递到视图
- 如何将变量数据放在数组中,javascript
- 将字符串与变量数据进行比较
- 删除变量数据时出错
- 如何将大型变量数据从javascript发送到SpringMVC控制器
- Express JS - 将后端变量/数据传递给 Angular JS
- 数组键变量正在更改,但变量数据没有更改
- 变量数据错误
- 将jQuery中的变量数据保存到PHP中,并在页面重新加载后获取
- 变量变量数据未发送到数据库中