簡體   English   中英

檢查數組是否為空 javascript

[英]check if array it´s empty javascript

I´m traying to create function to validate my form with jquery by response ajax and laravel backend.

在我的后端我有這個:

/**
     * SEARCH DATA CLIENT FOR CREATE PRECONTRATO
     */
    public function searchClient(Request $request)
    {
        $client = $request->get('documento');

        $clientSearch = Cliente::where('CIF_NIF', $client)->get();

        if($clientSearch->count() > 0){
            return $clientSearch[0];
        }
        
        return $clientSearch;
    }

如果查詢不返回任何結果,則此 function 返回客戶端或空集合。 我的問題是我無法檢查這種情況。

如果我放了一個在我的數據庫中不存在的數據,請返回:

[]
No properties

沒關系,不存在,但我success function ajax

我在 ajax 中有這個:

$.ajax({
    url: "{{ route('admin.precontratos.searchClient') }}",
    method: "POST",
    data: { "tipo_documento": tipoDocumento, "documento": documento, 
        "_token": token,
    },
    success: function(response){
        if(response != null || response != "" || response.length > 0 ){
            console.log(response.length);
            $("#apellido1_titular").hide();
            $("#apellido2_titular").hide();
            $("#apellido1_titular_label").hide();
            $("#apellido2_titular_label").hide();
            $("#caducidad_dni").val(changeFormatDate(response.CAMPOCONFC6));
            $("#nombre_titular").val(response.NOMBREFISCAL);

            if(response.MOVIL != "" || response.MOVIL != null){
                $("#tipo_telefono").val('movil');
                $("#telefono1").val(response.MOVIL);
            }
            if(response.TELEFONOS != "" || response.TELEFONOS != null){
                $("#tipo_telefono2").val('fijo');
                $("#telefono2").val(response.TELEFONOS);
            }
            $("#email_titular").val(response.E_MAIL);
            
            let date_born = response.CAMPOCONFC5;
            
            $("#fecha_nacimiento").val(changeFormatDate(date_born));
            $("#pais_nacimiento").val(response.CAMPOCONFC3);
            $("#nacionalidad").val(response.CAMPOCONFC4);

            if(response.CAMPOCONFC2 == "UNION LIBRE"){
                $("#estado_civil").val('pareja_de_hecho_convivencia');
            }
            if(response.CAMPOCONFC1 == "FIJO/DISCONTINUO"){
                $("#tipo_ocupacion").val("PRIVATE_EMPLOYEE");
                $("#ocupacion").val("PERMANENT_SEASONAL");
            }

            $("#nombre_calle").val(response.DIRECCION);
            $("#iban_ccc").val(response.IBAN);

            /** VALIDATION INPUTS FILLED */
            validateInputsPrecontrato();
        }else{
            console.log("entro");
        }

但總是返回console.log(response.length); 我需要我的 function go 阻止並且永遠不會進入那里。 我不知道我做錯了。

感謝您的幫助和自述文件

您正在對 web 服務器使用 XHR 請求。 您必須以正確的格式接受響應,因為您的后端代碼必須響應 JSON 格式的數據。

public function searchClient(Request $request) {

    $client = $request->get('documento');
    $clientSearch = Cliente::where('CIF_NIF', $client)->first();

    return $clientSearch 
    ? response()->json([
            'status' => 'success',
            'content' => $clientSearch
        ])
    : response()json([
            'status' => 'error',
            'content' => []
        ]);
}

在您的 Ajax 成功方法中,您可以檢查將狀態屬性設置為成功的響應

success: function(response){
        if(response.hasOwnProperty('status') && response.status === 'success') {
            // Your code logic for success
        } else {
            // Your code logic for error
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM