Here is a function return:
return response()->json(['aa'=>'bbb']);
and i print the function response ,the result like this:
JsonResponse {#186
#jsonOptions: 0
#data: "{"aa":"bbb"}"
#callback: null
#encodingOptions: 15
+headers: ResponseHeaderBag {#187
#computedCacheControl: array:1 [
"no-cache" => true
]
i have never seen it before,how can i get the value bbb
? thanks
json() The json() method of the Response interface takes a Response stream and reads it to completion. It returns a promise which resolves with the result of parsing the body text as JSON .
JSON ResponseThis method will automatically set the Content-Type header to application/json. The json method will automatically convert the array into appropriate json response.
i have resolved the question,use getData()
can read the json.
$a = response()->json(['aa'=>'bbb']);
$a->getData()->aa;
What you see is the object that response()->json()
produces. That's not actually what the client will get. Because Laravel will convert it into a string before sending it back.
On the client you can just use it as JSON. Here's an example with jQuery ajax:
$.ajax({
url: '/your/route'
}).done(function(data){
alert(data.aa); // alerts bbb
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With