简体   繁体   English

错误:语法错误:意外的标记{位于JSON中的位置4

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

I am using ajax to do some mathematical calculations. 我正在使用ajax进行一些数学计算。 The controller sends the correct response back but the data is not being displayed (when I try to do console.log(data) after success) and instead alerts an error message - "Error : SyntaxError: Unexpected token { in JSON at position 4". 控制器发送回正确的响应,但未显示数据(当我尝试在成功后尝试执行console.log(data)时),而是警告错误消息- "Error : SyntaxError: Unexpected token { in JSON at position 4". I have been debugging since yesterday to no success. 从昨天开始我一直在调试,但没有成功。

Here is the JSON being returned 这是返回的JSON

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

在此处输入图片说明

This is the Ajax function 这是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);
         }
     });
}

My Controller 我的控制器

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

Missing double quotes. 缺少双引号。 The JSON should be 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