[英]Issue with decoding json array in php
Okay now i am having a weird issue here, sometime back i learned how to encode and decode a json array properly here on stackoverflow but now i am having a weird issue on my godaddy server that i cannot comprehend, maybe i may have made i typo or something somewhere in code but i honestly cannot tell what is wrong here. 好的,现在我在这里遇到一个奇怪的问题,有时我在stackoverflow上学习了如何正确编码和解码json数组,但是现在我在我的Godaddy服务器上遇到了一个奇怪的问题,我无法理解,也许我犯了错字或代码中的某处,但老实说我无法分辨出这里出了什么问题。 The code works okay on my localhost but not when i upload it too my godaddy server. 该代码可以在我的本地主机上正常运行,但是当我也将其上载到Godaddy服务器上时则无法运行。
The code here is basically supposed to pass an id as a json to the php server which is then supposed to execute a query using the id as a parameter. 基本上,这里的代码应该将id作为json传递给php服务器,然后由php服务器使用id作为参数执行查询。
Here is the jquery code: 这是jQuery代码:
<script text="text/javascript">
$(document).ready(function() {
$('#download').click(function(e) {
// e.preventDefault();
var tid = $('#id').val().trim();
var data = {
ID:tid
};
$.ajax({
type: "POST",
url: "xxxxx-xxxxxxx.php",
data: {
data: JSON.stringify(data)
},
dataType: "json",
success: function(response) {
}
});
});
});
</script>
and this is the php code: 这是PHP代码:
<?php
if (isset($_POST['data']))
{
$Data = $_POST["data"];
$arr = json_decode($Data);
$id = $arr->ID;
$sql = $pdo->prepare("update ********** set ******** = ******** + 1 where id=:id");
$sql->bindValue("id", $id, PDO::PARAM_INT);
$sql->execute();
unset($_POST['data']);
}
?>
Now i checked if the value was being sent to the server using my browser console and it was. 现在,我检查了是否使用我的浏览器控制台将该值发送到服务器,并且确实如此。 I also checked if the $_POST['data'] on the server contained any data using var_dump and it did in fact have the data i wanted. 我还使用var_dump检查服务器上的$ _POST ['data']是否包含任何数据,并且实际上确实具有我想要的数据。
in the ajax set the content type to contentType: "application/json",
在ajax中,将内容类型设置为contentType: "application/json",
so: 所以:
$.ajax({
type: "POST",
url: "xxxxx-xxxxxxx.php",
data: {
data: JSON.stringify(data)
},
contentType: "application/json",
success: function(response) {
}
});
and in the php use: 并在php中使用:
$json = file_get_contents('php://input');
$data = json_decode($json);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.