错误:SyntaxError:意外的令牌{在位置4的JSON中

时间:2017-12-15 13:27:20

标签: javascript jquery ajax laravel-5

我正在使用ajax进行一些数学计算。控制器发回正确的响应,但数据没有显示(当我尝试在成功后尝试执行console.log(数据)),而是警告错误消息- "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
  }
}

enter image description here

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

1 个答案:

答案 0 :(得分:0)

缺少双引号。 JSON应该是

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