doubeng1278
2017-12-05 08:00
浏览 110
已采纳

json_decode期望参数1为字符串,表示给出了数组。 为什么我的字符串没有正确传递?

So, I've been struggling with this for the entire weekend and I still can't figure out what's wrong. I'm trying to pass some data through json_decode to be able to save it to a file and I keep getting the error it expects a string but an array is given. I'm using jQuery and PHP.

The data I send through the ajax call is, according to console.log(noBrack):

{"ID":2,"LLID":"LLID2","volNaam":"Test - 0","norm":"Zicht","instalDatum":"17-11-2017","endDate":"18-11-2017","serie":"0","klant":"Testklant","file":"data/Testklant (Heerenveen)/LLID2.json","gelDat":"27-10-2018"}

My Ajax call is:

$.ajax({
  url: 'quickGrade.php',
  type: 'POST',
  data: noBrack,
  datatype: 'json',
  success:function(data){
    alert(data );
  }
});

My PHP code is:

$testSave = 'data/gradeTest.json';
$decode = json_decode($_POST, true);
file_put_contents($testSave, $decode);

Can anyone find out what I'm doing wrong? I've tested my string with an online json_decode tester and it said it was valid so I'm kinda hardstuck here.

图片转代码服务由CSDN问答提供 功能建议

所以,我整个周末一直在努力解决这个问题,但我仍然无法弄清楚是什么问题。 我试图通过 json_decode 传递一些数据,以便能够将它保存到文件中,并且我不断得到它期望字符串的错误,但是给出了一个数组。 我正在使用jQuery和PHP。

我通过ajax调用发送的数据是,根据 console.log(noBrack) \ n

  {“ID”:2,“LLID”:“LLID2”,“volNaam”:“Test  -  0”,“norm”:“Zicht”,“instalDatum”:“17-11  -2017“,”endDate“:”18-11-2017“,”serie“:”0“,”klant“:”Testklant“,”file“:”data / Testklant(Heerenveen)/LLID2.json“,”  gelDat“:”27-10-2018“} 
   
 
 

我的Ajax调用是:

  $ .ajax  ({
 url:'quickGrade.php',
 type:'POST',
 data:noBrack,
 datatype:'json',
 success:function(data){
 alert(data);  
} 
}); 
   
 
 

我的PHP代码是:

  $ testSave ='data /  gradeTest.json'; 
 $ decode = json_decode($ _ POST,true); 
file_put_contents($ testSave,$ decode); 
   
 
 

任何人都可以找到什么 我做错了? 我已经用在线json_decode测试器测试了我的字符串,它说它有效,所以我在这里有点困难。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doufan1899 2017-12-05 08:06
    已采纳

    The way you are sending data will give you $_POST array in php code. So actually you do not need to decode because the data is coming as $_POST array not a JSON string.

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题