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.
Das console.log der Antwort lautet
{data: "{" error ":" Der Name muss mit mehr als einem… NULL↵ eingegeben werden
["isPipe": protected] => ↵ NULL↵} ↵} ↵ ", Status: 203, StatusText: " Nicht autorisierende Informationen ", Kopfzeilen: {…}, config: {…},…} config: {adapter: ƒ, transformRequest: {…}, transformResponse: {…}, timeout: 0, xsrfCookieName: "XSRF-TOKEN",…} data: "{" error ":" Name muss sein mit mehr als einem Zeichen eingegeben. "} object (Slim\Http\Response) # 32 (5) {↵ ["status": protected] => ↵ int (200) ↵ ["reasonPhrase": protected] => ↵ Zeichenfolge (0) "" ↵ ["protocolVersion": protected] => ↵ Zeichenfolge (3) "1.1" ↵ ["headers": protected] => ↵ object (Slim\Http\Headers) # 33 (1) {↵
["data": protected] => ↵ Array (1) {↵ ["Inhaltstyp"] => ↵
Array (2) {↵ ["Wert"] => ↵ Array (1) {↵ [0] => ↵
Zeichenfolge (24) "text/html; Zeichensatz = UTF-8" ↵} ↵
["originalKey"] => ↵ Zeichenfolge (12) "Inhaltstyp" ↵} ↵} ↵} ↵ ["body": protected] => ↵ object (Slim\Http\Body) # 31 (7) {↵
["stream": protected] => ↵ Ressource (59) vom Typ (Stream)
["meta": protected] => ↵ NULL↵ ["lesbar": geschützt] => ↵ NULL↵
["beschreibbar": geschützt] => ↵ NULL↵ ["suchbar": geschützt] => ↵
NULL↵ ["size": protected] => ↵ NULL↵ ["isPipe": protected] => ↵
NULL↵} ↵} ↵ "headers: {content-type: " Application/json; charset = utf-8 "} request: XMLHttpRequest {Onreadystatechange: ƒ, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload,…} status: 203 statusText: "Nicht autorisierende Informationen" proto : Objekt
JSON.parse (response.data) sowie response.data.error -> Beide geben einen Fehler aus. Wie kann ich die Daten lesen?
Slimframework 3.
$data = array('error' => 'Name must be entered with more than one character.');
$newResponse = $response->withJson($data, 203);
return $newResponse;
In Axios werden Antworten bereits als Javascript-Objekt bereitgestellt. Sie müssen nicht analysiert werden, erhalten einfach Antworten und greifen auf Daten zu.
Ich hatte eine ähnliche Formatantwort wie die im Konsolenprotokoll und mein Problem war, dass meine .json-Datei nicht als Eigentum formatiert wurde. Mir fehlte ein Komma. Posten Sie Ihre Json-Datei, um einen Blick darauf zu werfen.