douzhuo6931
2016-10-15 02:10
浏览 103

json_decode()不起作用

I am using ajax to send data with JSON and it keeps returning null. Here is the string I'm sending:

{"username":"HittmanA","code":"%601234567890-%3D~!%40%23%24%25%5E%26*()_%2Bqwertyuiop%5B%5D%5CQWERTYUIOP%7B%7D%7Casdfghjkl%3B'ASDFGHJKL%22zxcvbnm%2C%2FZXCVBNM%3C%3E%3F","name":"Untitled-1"}

It is sent via post. Here is the code I send it with:

       function slash(strr){
                            var re = /([^a-zA-Z0-9])/g; 
                            var str = strr;
                            var subst = '\$1'; 
                            var st = encodeURIComponent(str.replace(re,subst));
                            console.log("st");
                            return st;
                           }
          function create() {
            var info = {};
            var code=editor.getValue();
            info.username=username;
            info.code=slash(code);
            var name=document.getElementById('projectName').value;
            name2=name;
            info.name=slash(name2);
            info=JSON.stringify(info);
            console.log(info);
            var xhttp = new XMLHttpRequest();
            xhttp.onreadystatechange = function() {
              if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById("demo").innerHTML = xhttp.responseText;
              }
            };
            xhttp.open("POST", "create_project.php", true);
            xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xhttp.send("info="+info);
          }

When it gets received in the php file it is processed like this:

    $info = $_POST['info'];
    echo "<pre>".$info."</pre>";
    //$info = urldecode($info);
    $info = json_decode($info);
    echo "<pre>".$info."</pre>";

However for some reason the json_decode() doest work. Again here is the JSON I'm sending:

{"username":"HittmanA","code":"%601234567890-%3D~!%40%23%24%25%5E%26*()_%2Bqwertyuiop%5B%5D%5CQWERTYUIOP%7B%7D%7Casdfghjkl%3B'ASDFGHJKL%22zxcvbnm%2C%2FZXCVBNM%3C%3E%3F","name":"Untitled-1"}

the first echo works correctly but the second one doesn't. How do I fix this?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • douhong6187 2016-10-15 02:53
    已采纳

    json_decode() must be emitting an error which you are not checking. Functions like json_decode() and json_encode() do not display errors, you must use json_last_error and since PHP 5.5 there is also json_last_error_msg().

    <?php
    
    $str = '{"username":"HittmanA","code":"%601234567890-%3D~!%40%23%24%25%5E%26*()_%2Bqwertyuiop%5B%5D%5CQWERTYUIOP%7B%7D%7Casdfghjkl%3B\'ASDFGHJKL%22zxcvbnm%2C%2FZXCVBNM%3C%3E%3F","name":"Untitled-1"}';
    
    var_dump($str);
    var_dump(json_decode($str));
    var_dump(json_last_error());
    var_dump(json_last_error_msg());
    

    The above outputs:

    string(189) "{"username":"HittmanA","code":"%601234567890-%3D~!%40%23%24%25%5E%26*()_%2Bqwertyuiop%5B%5D%5CQWERTYUIOP%7B%7D%7Casdfghjkl%3B\'ASDFGHJKL%22zxcvbnm%2C%2FZXCVBNM%3C%3E%3F","name":"Untitled-1"}"
    class stdClass#1 (3) {
      public $username =>
      string(8) "HittmanA"
      public $code =>
      string(136) "%601234567890-%3D~!%40%23%24%25%5E%26*()_%2Bqwertyuiop%5B%5D%5CQWERTYUIOP%7B%7D%7Casdfghjkl%3B\'ASDFGHJKL%22zxcvbnm%2C%2FZXCVBNM%3C%3E%3F"
      public $name =>
      string(10) "Untitled-1"
    }
    int(0)
    string(8) "No error"
    

    If we try to decode invalid JSON:

    <?php
    
    $str = 'foobar{';
    
    var_dump($str);
    var_dump(json_decode($str));
    var_dump(json_last_error());
    var_dump(json_last_error_msg());
    

    The above prints:

    string(7) "foobar{"
    NULL
    int(4)
    string(16) "boolean expected"
    

    There must be an error in the JSON when you try to decode it. Check for errors usings the json_* error message functions. The error message will tell you what's wrong and it will be straight to fix once you know what the error is.

    已采纳该答案
    打赏 评论
  • douchuang4181 2016-10-15 02:17

    hello mister if you echo in php is equal to return or print in some functional programming. if your using ajax, ajax is one way communication.

       $info = $_POST['info'];
        //this code will be return 
        echo "<pre>".$info."</pre>";
        //this also will not be triggered cause you already return the above code
        //$info = urldecode($info);
        $info = json_decode($info);
        echo "<pre>".$info."</pre>";
    
    打赏 评论
  • dongtuo3530 2016-10-15 02:18

    Using json_decode like this:

    $info = json_decode($info);
    

    Will return a PHP variable.

    If you add the associative parameter as true (false by default) like this:

    $info = json_decode($info, true);
    

    Then it will return an associative array

    http://php.net/manual/en/function.json-decode.php

    打赏 评论
  • douzhang8144 2016-10-15 02:18

    Perhaps setting the xhttp content-type as json like

    <?PHP
    $data = /** whatever you're serializing **/;
    header('Content-Type: application/json');
    echo json_encode($data); ?>
    

    As stated in this answer.

    打赏 评论
  • doulan1866 2016-10-15 02:22

    Looking at the object it looks like you have a ' inside your code parameter. I think that's invalid for encoding.

    打赏 评论

相关推荐 更多相似问题