简体   繁体   中英

Handling Json response in Jquery

This is my script

<script type="text/javascript">

$(function() {
    $('#domain-checker').bind('submit',function(event){
        $.post('https://www.my-site.com/come/domainchecker.php',
        $(this).serialize(),
        function(json) {
            if(json.error) {
                $(".rezultat").text(json.error.message).fadeIn();
            } else if(json.result) {

                $(".rezultat").text("Domen je dostupan").fadeIn();
            } else {

                $(".rezultat").text("Domen je zauzet").fadeIn();
            }
        }, 'json');
        return false;
    });
});
</script>

This is response from my php script:

Domain is unavailable:

{
    "request":{
        "operation":"checkRegisterAvailability",
        "ip":"XXX.XXX.XXX.XXX"
    },
    "reply":{
       "code":"300",
       "detail":"success",
       "unavailable":{"domain":"google.com"}
    }
}

Domain is available:

{
   "request":{
       "operation":"checkRegisterAvailability",
       "ip":"XXX.XXX.XXX.XXX"
    },
    "reply":{
        "code":"300",
        "detail":"success",
        "available":{"domain":"johnsmithisthebest.com"}
    }
 }

When it's invalid:

{
     "request":{
         "operation":"checkRegisterAvailability",
         "ip":"XXX.XXX.XXX.XXX"
     },
     "reply":{
         "code":"300",
         "detail":"success",
         "invalid":{"domain":"asdasd.com.com"}
     }
}

I want to display different message for each of these responses with something like $(".rezultat").text("Domen je dostupan").fadeIn() I searched on internet about variables, values, json and jquery nothing helpful really. The most helpful article i came up with is this one: http://api.jquery.com/jquery.parsejson/ But because this is my first jquery attempt i am writing here. I do not know where to search.

Follow up to Jamiecs answer

<script type="text/javascript">

$(function() {
    $('#domain-checker').bind('submit',function(event){
        $.post('https://www.my-site.com/come/domainchecker.php',
        $(this).serialize(),
        function(json) {
            if(json.reply.invalid) {
    $(".rezultat").text("Domen " + json.reply.invalid.domain + " nije validan").fadeIn();
            } else if(json.reply.unavailable) {

                $(".rezultat").text("Domen nije dostupan").fadeIn();
            } else {

                $(".rezultat").text("Domen je dostupan").fadeIn();
            }
        }, 'json');
        return false;
    });
});
</script>

not printing anything.

EDIT:

I have added

header('Access-Control-Allow-Origin: *');

To domainchecker.php and it's working now.

When you layout your responses so they are easier to read (see question update) its clear that neither json.error nor json.result are ever in one of the three types or response. Also, json.error.message is never in any result... so the rest of this answer is a bit of a guess.

To determine if an "invalid" response came back you could do this:

if(json.reply.invalid){
    $(".rezultat").text("The domain " + json.reply.invalid.domain + " is invalid").fadeIn();
}

Similarly for an "unavailable" response

else if(json.reply.unavailable) {
    $(".rezultat").text("Domain is unavailable").fadeIn();
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM