.length belgilar soni, qator uzunligi emas

Json tomonidan qaytarilgan qatorni hisoblashni istayman. response.length dan foydalanganimda, qatordagi barcha belgilarni hisoblab chiqadi (men shuni taxmin qilamanki), men nima qilishni xohlasam qatorda qancha element borligini hisoblash. Xuddi shu uslub boshqa sahifalarda ham ishlaydi, bu bilan emas.

Bu php-kod:

            ...$response[] = array("id" => $row['appid'],
                            "hour" => $row['hour'],
                            "tname" => $tname,
                            "tsurname" => $tsurname,
                            "cname" => $cname,
                            "csurname" => $csurname,
                            "cgsm" => $cgsm,
                            "cemail" => $cemail,
                            "cdept" => $cdept,
                            "cdeg" => $cdeg,
                            "purpose" => $purpose,
                            "clientnotshown" => $row['clientnotshown']);


    };

    if(isset($response)){

    echo json_encode($response);

    } else {

    $response = array("val" => 0);
    echo json_encode($response);

    };

Javascript kodi:

function updateTable() {

var getData = {
                      date: $("#displaydate").val(),
                      operation:"getData"
                  }

                  $.post( "printoperations.php", getData).done(function( response ) {
                      if (response.val != 0){

                          alert("so far so good")
                          var arrayLength = response.length
                          alert(response)
                          alert(arrayLength)}
};

Here is a picture of what I get. I want to get the count of items, which in this case is 2.

1
JSONni haqiqiy array olish uchun ajralib chiqarmoqchi bo'ldingizmi? Yoki $. Post() deb ataganingizda, 'json' ma'lumot turini tanlang va jQuery sizni uni ko'rib chiqadi.
qo'shib qo'ydi muallif nnnnnn, manba
JSON.parse (javob) yoki JQuery-ga JSON javobini kuting.
qo'shib qo'ydi muallif Sven, manba
Ob'ektga javobni tahlil qilish va undan keyin hisoblash.
qo'shib qo'ydi muallif Ameya Deshpande, manba
juda johil bo'lgani uchun afsusdaman, lekin buni qanday qilishni bilmayman.
qo'shib qo'ydi muallif Ahmet, manba

6 javoblar

PHP'dan qaytib nima bo'lishidan qat'i nazar, har doim mag'lubiyatdir Agar jQuery siz uchun avtomatik ravishda bu mag'lubiyatni ajrata olsa, yoki to'g'ri sarlavhalarni yuboradigan bo'lsangiz yoki uning sozlamalarida JSON ekanligini aytsangiz

$.post("printoperations.php", getData, function(response) {
    if (response.val != 0) {
        console.log("so far so good")
        var arrayLength = response.length
        console.log(response)
        console.log(arrayLength)
    }
}, 'json');//<- here

Va disk raskadrovka uchun konsoldan foydalaning

3
qo'shib qo'ydi

Javobni var response = JSON.parse (javob) bilan ajrating va keyin response.length kerakli natijani qaytaradi.

2
qo'shib qo'ydi
var getData = {
                  date: $("#displaydate").val(),
                  operation:"getData"
              }

              $.post( "printoperations.php", getData).done(function( response ) {
                  if (response.val != 0){

                      alert("so far so good")
                      var responseArray = JSON.parse(response)
                      alert(responseArray)
                      alert(responseArray.length)}
};
2
qo'shib qo'ydi
@adeneo juda ko'p rahmat. U typo bo'ldi. uni tuzatdi
qo'shib qo'ydi muallif Ameya Deshpande, manba

Siz uzunlikni topishga urinayotganingizni sezasiz. Buning o'rniga alert (typeof response) ni ishlating, agar u biron bir narsani bajarmasdan oldin JSONga JSON.parse (response) bilan ajralib chiqsa va keyin harakat qilib ko'ring.

1
qo'shib qo'ydi

Buni qo'yish kerak

header('Content-Type: application/json');

server tomonidagi PHP so'rovida.

0
qo'shib qo'ydi

JSON.parse(response)That did the trick. Thanks for all answers!

0
qo'shib qo'ydi
Javascript UZB
Javascript UZB
99 ishtirokchilar

@js_uzb @vuejs_uz @react_uz @nodejs_uz @angular_uz @ngTashkent @yiiframework_uz @laravel_uz @linux_uzbek @python_uz @swift_uzb —————— @uzdevgroup @UzGeeksGroup ——— @UzDev_Jobs @jobs_uzb

PhP |BotsUz
PhP |BotsUz
93 ishtirokchilar

Phpni o'rganishni Hohlasangiz https://t.me/joinchat/AAAAAE-KRc5dd5tPMmGmWA A'zo bo'lin