簡體   English   中英

錯誤:語法錯誤:意外的標記{位於JSON中的位置4

[英]Error : SyntaxError: Unexpected token { in JSON at position 4

我正在使用ajax進行一些數學計算。 控制器發送回正確的響應,但未顯示數據(當我嘗試在成功后嘗試執行console.log(data)時),而是警告錯誤消息- "Error : SyntaxError: Unexpected token { in JSON at position 4". 從昨天開始我一直在調試,但沒有成功。

這是返回的JSON

{
  mydata: {
    items: [
      20.68
    ],
    sub_total: 20,
    tax_total: 0.68,
    grand_total: 20.68
  }
}

在此處輸入圖片說明

這是Ajax功能

function totalItem() {
    $.ajax({
        url: '{{ url("finance/items/totalItem") }}',
        type: 'POST',
        dataType: 'JSON',
        data: $('#items input[type=\'text\'],#items input[type=\'hidden\'], #items textarea, #items select'),
        headers: { 'X-CSRF-TOKEN': csrf },
        success: function(data) {
            if (data) {
                $.each( data.items, function( key, value ) {
                    console.log(data);
                    //$('#item-total-' + key).html(value);
                    $('#item-total-' + key).val(value);
                });
                $('#sub-total').html(data.sub_total);
                $('#tax-total').html(data.tax_total);
                $('#grand-total').html(data.grand_total);
            }
         },
         error:function (xhr, ajaxOptions, thrownError) {
             alert("Error : "+thrownError);
         }
     });
}

我的控制器

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]);
        }
    }

缺少雙引號。 JSON應該是

{
  "mydata": {
    "items": [
      20.68
    ],
    "sub_total": 20,
    "tax_total": 0.68,
    "grand_total": 20.68
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM