Jquery Ajax没有回复

时间:2015-12-17 11:50:34

标签: javascript jquery json ajax

我有以下jQuery脚本应该从我的服务器获取数据:

$(".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);
        }
    });
});

返回的数据位于以下模拟数据中:

{
  "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"
}

如何从响应中获取数据并在屏幕上提醒它?

3 个答案:

答案 0 :(得分:0)

您的网址可能存在拼写错误。在IP地址的最后一部分之后没有正斜杠。

url: "http://192.168.0.12

答案 1 :(得分:0)

你的ajax网址在ip和文件夹名http://192.168.0.12 /之间缺少/ LMISWebservices /...

试试这个

$(".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);
                    }
                });
            });

答案 2 :(得分:0)

我想你应该试试这个。这不是在这里运行的,因为跨域但是尝试使用完美的代码。

$(".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">