如何在 jquery 中从 Web 服务获取响应

How can I get response from webservice in jquery?

本文关键字:服务 获取 响应 Web 中从 jquery      更新时间:2023-09-26

我想从我的登录网络服务获取数据响应。这是我的网络服务 http://41.128.183.109:9090/api/Data/getloginer?medid=a&pass=a (用于测试)。这是我的代码:

$("#login").click(function () {
    var url = "http://41.128.183.109:9090/api/Data/getloginer?medid=a&pass=a";
    $.ajax({
        url: url,
        type: 'Get',
        success: function (data) {
            alert(data.Medical_id);
        },
    });
});

alert()向我展示了undefined.请告知问题可能是什么。

结果是一个数组,所以为了获得该字段,你必须迭代结果或获取第一项:

for (var i in data) { 
   console.log(d[i].Medical_id);
}

或者您可以简单地得到第一个结果:

$.ajax({
  url: 'http://41.128.183.109:9090/api/Data/getloginer?medid=a&pass=a',
  type: 'Get',
  success: function (data) {
        alert(data[0].Medical_id);
  }
});
The JSON  result you are getting is :
[{"$id":"1","id":8,"Medical_id":"a","Password":"a","Name":null,"Email":null,"gender":null,"Type":null}]
use for each to iterate through  the results  or 
instead of this  you  can check the result like this :    
var data = [{ "$id": "1", "id": 8, "Medical_id": "a", "Password": "a", "Name": null, "Email": null, "gender": null, "Type": null }] 
alert(data[0].Medical_id);

如果您在 localhost 中开发应用程序,那么也请参阅此问题。
"请求的资源上不存在'访问控制-允许源'标头"


如果您使用的是 chrome,请使用此链接使用 CORS 启用插件。
https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en-US
希望这会有所帮助