【发布时间】:2020-07-16 19:47:13
【问题描述】:
有以下js:
var json = JSON.stringify(DispAplazados);
console.log(json);
$.ajax({
url: API_URL + '/distanciamiento/alerta'
, type: 'POST'
, contentType: 'application/json'
, data: {data:json} //stringify is important
, headers: {
'Authorization': 'Basic ' + btoa(API_USERNAME + ':' + API_PASSWORD)
}
, success: function(res)
{
if (res.status == 'OK')
{
cordova.plugins.notification.local.schedule({
title: 'Alerta',
text: 'Se mando una alerta a la BD...' + DispAplazados[0].Hashid,
foreground: true
});
console.log(res);
}
else
{
console.log(res);
}
}
});
在苗条(php)中我已经完成了:
print_r($_REQUEST, TRUE);
但是数组是空的。 json字符串化对象的日志如下:
[{"name":"moto g(7) play","id":"63:D3:FC:7E:C9:32","advertising":{},"rssi":-60,"distancia_aproximada":1.1352362990362899,"Hashid":"6a5caba3-f30b-cbbf-3726-f11851500422","dispositivo_central":"d8504723c61da70","acumulador_segundos":60}]
我在 ajax 调用中做错了吗?
【问题讨论】:
-
你检查过 php://input 的内容了吗? var_dump(file_get_contents("php://input"));
-
我用过 $chd = json_decode(file_get_contents('php://input'), TRUE); $childs = implode("",$chd);然后在 ajax 调用中返回它并给我 null
-
首先检查
file_get_contents('php://input')实际返回的内容。