Ich verwende Ajax, um einige mathematische Berechnungen durchzuführen. Der Controller sendet die richtige Antwort zurück, aber die Daten werden nicht angezeigt (wenn ich versuche, console.log (Daten) nach Erfolg) und stattdessen eine Fehlermeldung - "Error : SyntaxError: Unexpected token { in JSON at position 4".
Ich habe seit gestern zu keinem Erfolg Debugging.Fehler: SyntaxError: Unerwartetes Token {in JSON an Position 4
Hier wird die JSON
{
mydata: {
items: [
20.68
],
sub_total: 20,
tax_total: 0.68,
grand_total: 20.68
}
}
Dies ist die Funktion
public function totalItem(Request $request)
{
//
//$input_items = request('item');
$input_items = $request->item;
$mydata = new \stdClass;
$sub_total = 0;
$tax_total = 0;
//$tax = 0;
$items = array();
if ($input_items) {
foreach ($input_items as $key => $item) {
//return response()->json($item['tax_id']);
$item_tax_total = 0;
$price = isset($item['price']) ? $item['price'] : 0;
$qty = isset($item['quantity']) ? $item['quantity'] : 0;
$item_sub_total = ($price * $qty);
//$item_sub_total = (2 * 2);
if (isset($item['tax_id'])) {
$tax = Tax::find($item['tax_id']);
$rate = isset($tax->rate) ? $tax->rate : 0;
$item_tax_total = (($price * $qty)/100) * $rate;
}
$sub_total += $item_sub_total;
$tax_total += $item_tax_total;
$total = $item_sub_total + $item_tax_total;
//$items[$key] = money($total, $currency_code, true)->format();
$items[$key] = $total;
//return response()->json($tax);
//return response()->json($sub_total);
}
}
$mydata->items = $items;
$mydata->sub_total = $sub_total; //money($sub_total, $currency_code, true)->format();
$mydata->tax_total = $tax_total; //money($tax_total, $currency_code, true)->format();
$grand_total = $sub_total + $tax_total;
$mydata->grand_total = $grand_total; //money($grand_total, $currency_code, true)->format();
return response()->json(['mydata' => $mydata]);
}
}
Kannst du bitte den JSON zeigen? –
Analysieren Sie den vom Server zurückgegebenen JSON mit einem JSON-Validator (google schlägt https://jsonlint.com/ vor). Häufige Fehler sind '' 'oder Kommas. Beachten Sie, dass nur doppelte Anführungszeichen gültige JSON sind und alle Schlüssel in Anführungszeichen eingeschlossen werden müssen. Das Problem ist dort – ilmirons
@FedericoklezCulloca Ich habe den JSON hinzugefügt, der zurückgegeben wird – radioactive