用ajax + json检查用户名

I made a php file which contains usernames in json format.

[
    {"korisnicko_ime":"darbranis"},
    {"korisnicko_ime":"markrc"},
    {"korisnicko_ime":"leoluk"},
    {"korisnicko_ime":"borbau"},
    {"korisnicko_ime":"ivavad"},
    {"korisnicko_ime":"andtikv"},
    {"korisnicko_ime":"nikohras"},
    {"korisnicko_ime":"marbranis"},
    {"korisnicko_ime":"ratkolev"},
    {"korisnicko_ime":"antobrk"}
]

Now i want to check with ajax + json if the username already exists in the database whick should look like this:

    var kor_ime = $("#k_ime").val();

    $.ajax({
        url: 'http://barka.foi.hr/WebDiP/2017_projekti/WebDiP2017x021/popis_korisnika.php',
        type: 'GET',
        dataType: 'json',
        success: function (json) {
            $.each(json, function (korisnicko_ime, value) {
                if (value === kor_ime) {
                    $("#poruke").text("Korisnik postoji!");
                    console.log("Korisnik postoji!");
                } else {
                    $("#greske").text("Korisnik ne postoji!");
                    console.log("Korisnik ne postoji");
                }

                console.log(kor_ime);
            });
        }
    });

Now when i focusout i get everytime the message that the username doesn't exists. I need help.

You have 2 errors in your javascript:

First one: you expect the variable value to be a string like "darbranis". However $.each will give the first parameter to be the index (0, 1, 2 and so on in this case since json is a regular array) and as the second parameter the value ({"korisnicko_ime":"darbranis"} for the first entry).

Thus:

$.each(json, function (korisnicko_ime, value) {
    if (value === kor_ime) {

Should be something like:

$.each(json, function (arrayidx, arrayvalue) {
    if (arrayvalue.korisnicko_ime === kor_ime) {

Second error: $("#poruke") would be written if there is a username within the json equals to kor_ime. $("#greske") would be written if there is a username within that json that doesn't equal to kor_ime. Since there are a lot of usernames within the json that doesn't equal kor_ime, $("#greske") would ALWAYS be written. I assume that "Korisnik ne postoji!" translates to something like "User not found", so you most likely won't like that behaviour. To fix this:

// Initialize userfound as false
var userfound = false;
$.each(json, function (arrayidx, arrayvalue) {
    if (arrayvalue.korisnicko_ime === kor_ime) {
        // If there is one entry with the correct username, the user is found
        userfound = true;
    }
});

// After the each check if user is found (one match) or not 
// found (no match at all, thus still at initial value false)
if (userfound) {
    $("#poruke").text("Korisnik postoji!");
    console.log("Korisnik postoji!");
} else {
    $("#greske").text("Korisnik ne postoji!");
    console.log("Korisnik ne postoji");
}

Complete demo in below code-snippet:

var kor_ime = 'markrc';
var json = [
    {"korisnicko_ime":"darbranis"},
    {"korisnicko_ime":"markrc"},
    {"korisnicko_ime":"leoluk"}
];

var userfound = false;
$.each(json, function (arrayidx, arrayvalue) {
    if (arrayvalue.korisnicko_ime === kor_ime) {
        userfound = true;
    }
});

if (userfound) {
    $("#poruke").text("Korisnik postoji!");
    console.log("Korisnik postoji!");
} else {
    $("#greske").text("Korisnik ne postoji!");
    console.log("Korisnik ne postoji");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="poruke" style="color: green;"></div>
<div id="greske" style="color: red;"></div>

</div>

change this way:

$.each(json, function (korisnicko_ime, value) {
            if (value === kor_ime) {
                $("#poruke").text("Korisnik postoji!");
                console.log("Korisnik postoji!");
            } else {
                $("#greske").text("Korisnik ne postoji!");
                console.log("Korisnik ne postoji");
            }

to this:

json.forEach(function (value) {

    if (value.korisnicko_ime == kor_ime) {
                $("#poruke").text("Korisnik postoji!");
                //console.log("yes");
       } else {
                $("#greske").text("Korisnik ne postoji!");
                //console.log("no");
       }
 });

So apparently your code expects an array with the user names, but this is not what you are getting. You are getting an array of objects, with a property korisnicko_ime that carries the user name.

Instead of checking if (value == kor_ime) you should be checking if (value.korisnicko_ime == kor_ime)

Also, you are not looking through all the entries before determining that the username doesn't exist. You perform the actions for non-existing user for every entry where the username mismatches.

But never mind that, why the f are you providing an unauthenticated user with a list of all possible logins on your system? You realize anyone can see that list right? This is a serious security breach. Do not validate sensitive information on client-side.