PHP ajax request processing returns json format

php return code format

public function json(){

        if (request()->isAjax()){
            $data = [
                'code'=>'1',
                'msg'=>'成功',
                'data'=>array('1'=>'1','2'=>'2'),
            ];
            return json_encode($data,JSON_UNESCAPED_UNICODE);
        }
        return view('json');
    }

ajax request

// listening Submit 
form.on ( 'Submit (UPD)', function (Data) {
     // the console.log (Data); 
    // send asynchronous data submitted to the PHP 
    $ .ajax ({ 
      URL: "{: URL ( 'JSON')} " , 
      type: " POST " , 
      Data: {}, 
      jsonType: " JSON " , 
      Success: function (A) {
         // var Movie = the eval (" ( "A + +") "); 
        the eval ( "var = Movie" + A); 
        the console.log (Movie); 
      }, 
      error: function (E) { 
            layer.alert ( "System error, please refresh the page re-edit " , {
                icon: . 7 
            }, 
            function () {
                 // Close the current window 
                layer.close (layer.index);
                 // refresh the page 
                window.parent.location.reload (); 
            }); 
        } 
    }) 
    return  to false ; 
});

Print results

 

 

 

 

Mainly eval () function is the key

{name: ' Ordinary World ', author: ' Lu Yao '} This is your json string data returned by the server, then you client you how to use it, eval is to execute the next string, so common string became an object (like say not very appropriate), after ordinary character is executed, which things will turn code can be used directly: such as date objects, arrays. . .
eval ( "var movie =" + result); phrase means that becomes a target of the string can be used directly
after performing equivalent
var movie = {name: ' Ordinary World ', author: ' LU Yao '}; the json free to assign variables to movie
then you can directly reference the movie.name is "ordinary World" is a movie.author " Lu Yao ", I hope you can understand

 

Guess you like

Origin www.cnblogs.com/lcxin/p/11453997.html