简体   繁体   中英

Jquery Ajax not giving a response

I have the following jQuery script which is supposed to fetch data from my server:

$(".login_button").click(function () {
    var username = $(".username").val();
    var userkey = $(".userkey").val();
    $.ajax({
        type: "GET",
        url: "http://192.168.0.12LMISWebservices/Validation?username=" + username + "&userkey=" + userkey + "",
        dataType: "JSON",
        success: function (response) {
            var program = response.program_code;
            alert(program);


        },
        error: function (response) {
           console.error(response);
        }
    });
});

And the data returned is in the following mock data:

{
  "Programs": [
    {
      "program_code": "Malaria",
      "program_name": "Malaria",
      "program_id": 2
    },
    {
      "program_code": "CD4",
      "program_name": "Laboratory Monitoring Reagents",
      "program_id": 6
    },
    {
      "program_code": "LAB",
      "program_name": "Test Kits",
      "program_id": 8
    },
    {
      "program_code": "ART",
      "program_name": "ART ",
      "program_id": 3
    },
    {
      "program_code": "TB & Leprosy",
      "program_name": "TB & Leprosy",
      "program_id": 5
    },
    {
      "program_code": "Nutri",
      "program_name": "Nutrition",
      "program_id": 7
    },
    {
      "program_code": "FP",
      "program_name": "Family Planning",
      "program_id": 1
    },
    {
      "program_code": "EMMS",
      "program_name": "Essential Medicines & Medical Supplies",
      "program_id": 4
    },
    {
      "program_code": "test3",
      "program_name": "FP Test",
      "program_id": 15
    }
  ],
  "facility_name": "",
  "profile_message": "ok",
  "mfl_code": "",
  "user_status": true,
  "facility_id": "",
  "login_as": "Patrick K M"
}

How can I get the data from the response and alert it on my screen?

There could be a typo in your URL. You have no forward slash after the last part of the IP address.

url: "http://192.168.0.12

Your ajax url is missing a / between ip and folder name http://192.168.0.12 / LMISWebservices/...

Try This

$(".login_button").click(function () {
                var username = $(".username").val();
                var userkey = $(".userkey").val();
                $.ajax({
                    type: "GET",
                    url: "http://192.168.0.12/LMISWebservices/Validation?username=" + username + "&userkey=" + userkey + "",
                    dataType: "JSON",
                    success: function (response) {
                        console.log(response);
                        //var program = response.program_code;
                        //alert(program);


                    },
                    error: function (response) {
                       console.error(response);
                    }
                });
            });

I think you should try this. this is not run here because cross domain but try in your code that work perfectly.

 $(".login_button").click(function() { var username = $(".username").val(); var userkey = $(".userkey").val(); $.ajax({ type: "GET", url: "http://192.168.0.12/LMISWebservices/Validation?username=" + username + "&userkey=" + userkey + "", dataType: "JSON", success: function(response) { //your JSON give list there for male loop throug each. $.each(response.Programs,function(i,item){ alert(item.program_code); }); }, error: function(response) { console.error(response); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <input type="text" class="username"/> <input type="text" class="userkey"/> <input type="button" value="GET" class="login_button"> 

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