Axios 0.17.1
.then(function (response) {
console.log(response);
//console.log(response.status);
//It is an error -> SyntaxError: Unexpected token u in JSON at position 0
console.log(JSON.parse(response.data.error));
console.log(response.data.error); //undefined.
响应的 console.log 是
{data: "{"error":"Name 必须输入多个… NULL↵
["isPipe":protected]=>↵ NULL↵ }↵}↵", status: 203, statusText: "Non-Authoritative Information ", headers: {...}, config: {...}, ...} config: {adapter: ƒ, transformRequest: {...}, transformResponse: {...}, timeout: 0, xsrfCookieName: "XSRF-TOKEN", ...} 数据: "{"error":"名称必须输入一个以上字符。"}object(Slim\Http\Response)#32 (5) {↵ ["status":protected]=>↵ int(200)↵ ["reasonPhrase":protected]=>↵ string(0) ""↵ ["protocolVersion":protected]=>↵ string(3) "1.1"↵ ["headers":protected]=>↵ object(Slim\Http \Headers)#33 (1) {↵
["data":protected]=>↵ 数组(1) {↵ ["内容类型"]=>↵
数组(2) {↵ ["值"]=>↵ 数组(1) {↵ [0]=>↵
string(24) "text/html; charset=UTF-8"↵ }↵
["originalKey"]=>↵ string(12) "Content-Type"↵ }↵ }↵ }↵ ["body":protected]= >↵ object(Slim\Http\Body)#31 (7) {↵
["stream":protected]=>↵ 类型 (stream) 的资源(59)↵
["meta":protected]=>↵ NULL↵ [ "可读":protected]=>↵ NULL↵
["writable":protected]=>↵ NULL↵ ["seekable":protected]=>↵
NULL↵ ["size":protected]=>↵ NULL↵ ["isPipe ":protected]=>↵
NULL↵ }↵}↵" headers : {content-type: "application/json;charset=utf-8"} 请求: XMLHttpRequest {onreadystatechange: ƒ, readyState: 4, timeout: 0, withCredentials : 错误的, 上传:XMLHttpRequestUpload,...} 状态:203 statusText:“非权威信息”原型:对象
JSON.parse(response.data) 以及 response.data.error -> 两者都给出错误。我如何读取数据?
3. 瘦身框架
$data = array('error' => 'Name must be entered with more than one character.');
$newResponse = $response->withJson($data, 203);
return $newResponse;