服务器将javascript对象写成字符串而不是整数

Server writing out javascript object as string instead of integer

本文关键字:整数 字符串 javascript 对象 服务器      更新时间:2023-09-26

我有一个奇怪的问题,我现在不能解决。

我的本地站点(homestead)在页面中编写javascript对象,如

var carriageCosts = [{"id":1,"weight":0,"cost":12,"created_at":"2015-10-20 06:25:13","updated_at":"2015-10-20 06:25:13"}]

Weight和cost都是整数。在使用forge部署到digitalocean后,它被这样写出来

var carriageCosts = [{"id":"1","weight":"0","cost":"12","created_at":"2015-10-21 02:49:37","updated_at":"2015-10-21 02:49:37"}]

重量和成本现在是字符串!!

这会导致javascript出错。有人见过这个吗?如果您能指点一下,我将不胜感激。

数据像这样发送到刀片视图

return view('quote.create')
    ->with([
        'customers' => $customers,
        'sellPrices' => 'App'SellPrice::all(),
        'coreCaps' => 'App'CoreCap::where('available', 1)->get(),
        'threePhaseCostings' => 'App'ThreePhaseCosting::all()->first()
    ]);

刀片视图写入数据

var carriageCosts = {!! $carriageCosts !!};

数据库中的Weight和Cost列都是整型。

开发服务器返回具有正确数据类型的Eloquent对象,即整型就是整型。生产服务器返回的Eloquent对象的数据类型不正确,即int是字符串。

在您的json_encode()可以使用选项JSON_NUMERIC_CHECK

echo json_encode($arr, JSON_NUMERIC_CHECK);

参考php json常量