【问题标题】:Json not getting to serverJson没有到达服务器
【发布时间】: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')实际返回的内容。

标签: php jquery json slim


【解决方案1】:

尝试将Ajax调用中的数据改为:

,    data: json

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-15
    • 1970-01-01
    • 2022-10-04
    • 2022-06-12
    • 2021-11-28
    • 1970-01-01
    • 2019-12-15
    • 1970-01-01
    相关资源
    最近更新 更多