如何将几个参数从服务器发送到js映射

时间:2019-04-05 15:13:17

标签: jquery laravel-5

在我的laravel 5.7 / jQuery 3应用程序中,我需要从服务器发送几个参数

$retArray= ['error_code' => 0, 'message' => ''];

foreach( $requestData as $next_param_name ) {
    if( $next_param_name == 'customerAccountTypeValueArray' ) {
        $retArray['customerAccountTypeValueArray']= Customer::getCustomerAccountTypeValueArray(false);
    }
    if( $next_param_name == 'customerStatusValueArray' ) {
        $retArray['customerStatusValueArray']= Customer::getCustomerStatusValueArray(false);
    }
}

return response()->json( $retArray, 200);

和数组retArray的数据如下:

Array
(
    [error_code] => 0
    [message] => 
    [customerAccountTypeValueArray] => Array
        (
            [I] => Individual
            [B] => Business
        )

    [customerStatusValueArray] => Array
        (
            [A] => Active
            [I] => Inactive
            [N] => New
        )

)

但是我遇到了Javascript错误:

Error in render: "TypeError: selectionsList.map is not a function" found in

带有我的JS代码:

axios.post('/ api / dashboard-settings',paramsArray)     .then((response)=> {

selectionsList= response.data.customerAccountTypeValueArray,
selectionsList.map((nextSelection, index) => {

看起来就像selectionsList不是元素数组,正如我所期望的那样。哪种方法正确?

已修改: 这就是我在浏览器控制台中查看来自服务器的数据的方式:https://imgur.com/a/jH6Juyo 然后我将其设置为参数。 谢谢!

1 个答案:

答案 0 :(得分:2)

尝试返回带有数据约束的对象数组:

        $tempDataArray= Model::getSomeDataArray();
        $retArray= [];
        foreach( $tempDataArray as $next_key=>$next_value ) {
            $retArray[]= (object)$next_value;
        }
        return response()->json( $retArray, 200);