简体   繁体   English

Ajax + JSON Decode + Slim PHP问题?

[英]Ajax + JSON Decode + Slim PHP problems?

Ok I can't seem to decode the JSON from my ajax call, which posts user data to my api, which is built in slim php.. 好吧,我似乎无法从我的ajax调用解码JSON,它将用户数据发布到我的api,它是用超薄的php构建的。

This is my ajax.. 这是我的ajax ..

var jsonData;
jsonData = [
      {
        username: "user",
        password: "pass"
      }
    ];

$.ajax({
  type: "POST",
  url: "http://localhost/api/user/auth",
  data: {
    user: JSON.stringify(jsonData)
  },
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(data) {
    alert("You are good!");
  },
  error: function(xhr, type) {
    alert("Y U NO WORK?");
  }
});

This is my SLIM PHP code.. 这是我的SLIM PHP代码..

$app->post('/user/auth', function () use ($app) {
    try {
         $requestBody = $app->request()->getBody(); //This works

         //RequestBody is: user=%5B%7B%22username%22%3A%22user%22%2C%22password%22%3A%22pass%22%7D%5D         

         $json_a = json_decode($requestBody); //This doesn't work

         print_r($json_a); //Has no output?

         $username = $json_a['user']['username']; //Therefore this doesn't work?

    } catch(Exception $e) {
         echo '{"error":{"text": "'. $e->getMessage() .'"}}';
    }
});

As you can see in the comments, written in the code, requestBody equals: 正如您在注释中所看到的那样,requestBody等于:

user=%5B%7B%22username%22%3A%22user%22%2C%22password%22%3A%22pass%22%7D%5D  

However, I can't seem to decode it, as print_r($json_a) has no effect. 但是,我似乎无法解码它,因为print_r($ json_a)没有效果。

Any help is much appreciated, thanks. 非常感谢任何帮助,谢谢。

Try 尝试

$params_str = urldecode($requestBody);
parse_str($params_str, $params_arr);
$user = json_decode($params_arr['user']);

Youre doing it wrong... you are sending the data via post so you shoudl grab the json string from there instead of trying to manually read the request body... something like 你做错了...你是通过邮件发送数据所以你shoudl从那里抓住json字符串而不是试图手动读取请求正文...类似的东西

$req = $app->request();
$json = json_decode($req->post('user'));

Now if you actually want to send a json request body thats a whole different thing, but it will require changes to your js. 现在,如果你真的想发送一个完全不同的json请求体,但它需要更改你的js。 You need to set processData to false so that it does not try to encode the data value internally. 您需要将processData设置为false,以便它不会尝试在内部对data值进行编码。 This also means you have to pre-encode it: 这也意味着你必须对它进行预编码:

$.ajax({
  type: "POST",
  url: "http://localhost/api/user/auth",
  data: JSON.stringify({user: jsonData}), // gotta strinigfy the entire hash
  processData: false,
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(data) {
    alert("You are good!");
  },
  error: function(xhr, type) {
    alert("Y U NO WORK?");
  }
});

The problem was the "user" key. 问题是“用户”密钥。 It wasn't formatted correctly for json decode. 它没有正确格式化为json解码。 I removed this entirely, as it wasn't necessary anyway. 我完全删除了它,因为它无论如何都没有必要。

So I changed: 所以我改变了:

data: {
    user: JSON.stringify(jsonData)
  },

to

 data: JSON.stringify(jsonData)

And, I also implemented @air4x line: 而且,我还实现了@ air4x系列:

$json_a = json_decode(urldecode($requestBody));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM