Ajax call says Failed to load resource: the server responded with a status of 500 (Internal Server Error) in laravel

Without ajax call If I call api with the help controller function then the api function is working fine and giving me the data, but If I call the same function with ajax call then I am getting the above error. Please help me to solve this issue.

const dateInput= document.getElementById('dateInput');  
const selectedDate = new Date(dateInput.value); 
    
$.ajax({
    type: 'get',
    url: '{{route("demo.function")}}',
    data: {
        'date' : selectedDate.toISOString().split('T')[0]
    },
    success: function(data) {
    var info = data.info;
    console.log("Information: " + info);
},
    error: function(error) {
        console.log(error,'There is some error here!');
    }
});

If demo.function route is callilng from my controller, demoFunction will be called when I am working with databased it working fine. But When I am doing the below thing, it’s not working: Failed to load resource: the server responded with a status of 500 this error I am getting. but when I testing api to get the data it working fine. but when with help of ajax request I am calling the function for getData it’s not wokring.

After calling the function in controller like this:

public function demoFunction(Request $request){
        $date = $request->input('date');
        $info = getData($date); // Api call function
        return response()->json(['message' => 'Data processed successfully', 'info'=> $info]);
    }

For getting Data from api:

public function getData($date){

    for($i=0; $i<21; $i++){
        $Payload = {
            // my data
        }
        
        try {
                    // Send the request
                    $response = $client->request('POST', 'http://xx.xx.xx.xx/my-ws/dynamic-data.html', [
                        'headers' => [
                            'Content-Type' => 'text/xml; charset=utf-8',
                        ],
                        'body' => $Payload,
                    ]);
        } catch (Exception $e) {
                    return response()->json(['error' => 'Request is not successfull'], 500);
        }

        $info[] = [
            'info' => $response->getBody();
        ];
    }

    return $info;
}

Thanks in Advance.