Generic Function Returning Null

Discussão em 'Angular' iniciado por user2780362, Agosto 4, 2025.

  1. user2780362

    user2780362 Guest

    I have a generic function that makes an ajax call. When I do a console.log in the success. The object prints out correctly. But when another function calls that function and tries to assign it to a variable and I do a console.log of the variable. It keeps coming up null. Can't seem to figure out why this is. But I'm new to TypeScript, so I might be missing something.

    Any help would be appreciated.

    private ajax<T>(url: string, verb: string, data: string | undefined): T | null {
    $.ajax({
    headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    },
    url: url,
    type: verb,
    dataType: 'json',
    data: data,
    success: function (response: T) {
    console.log(response);
    return response || null;
    },
    error: function (error) {
    console.log(error);
    }
    });

    return null;
    }

    ajaxGet<T>(url: string, data?: any | undefined): T | null {
    let content: string = (data !== undefined) ? JSON.stringify(data) : JSON.stringify('');
    let value: T | null = this.ajax<T>(url, 'GET', content);
    console.log(value);

    return value;
    }


    The console.log(response); line prints out the object in the Chrome debugger, but the console.log(value) line comes up null.

    Continue reading...

Compartilhe esta Página