如何从jquery中的webservice获得响应?

时间:2016-01-18 08:34:07

标签: javascript jquery asp.net-mvc web-services

我想从我的登录网络服务获取数据响应。这是我的网络服务  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。请告知问题所在。

3 个答案:

答案 0 :(得分:0)

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

<aside class="frame">                    

                   <?php  if ( is_page( 1757 ) ) { ?>

                             <?php the_content();?>

                   <?php } else{ ?>
                                <h1><?php the_title();?></h1>
                                <?php the_content();?>
                   <?php }?>

 </aside>

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

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

答案 1 :(得分:0)

var valKeyDown;
var valKeyUp;


function integerOnly(e) {
    e = e || window.event;
    var code = e.which || e.keyCode;
    if (!e.ctrlKey) {
        var arrIntCodes1 = new Array(96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 8, 9, 116);   // 96 TO 105 - 0 TO 9 (Numpad)
        if (!e.shiftKey) {                          //48 to 57 - 0 to 9 
            arrIntCodes1.push(48);                  //These keys will be allowed only if shift key is NOT pressed
            arrIntCodes1.push(49);                  //Because, with shift key (48 to 57) events will print chars like @,#,$,%,^, etc.
            arrIntCodes1.push(50);
            arrIntCodes1.push(51);
            arrIntCodes1.push(52);
            arrIntCodes1.push(53);
            arrIntCodes1.push(54);
            arrIntCodes1.push(55);
            arrIntCodes1.push(56);
            arrIntCodes1.push(57);
        }
        var arrIntCodes2 = new Array(35, 36, 37, 38, 39, 40, 46);
        if ($.inArray(e.keyCode, arrIntCodes2) != -1) {
            arrIntCodes1.push(e.keyCode);
        }
        if ($.inArray(code, arrIntCodes1) == -1) {
            return false;
        }
    }
    return true;
}

$('.integerOnly').keydown(function (event) {
    valKeyDown = this.value;
    return integerOnly(event);
});

$('.integerOnly').keyup(function (event) {          //This is to protect if user copy-pastes some character value ,..
    valKeyUp = this.value;                          //In that case, pasted text is replaced with old value,
    if (!new RegExp('^[0-9]*$').test(valKeyUp)) {   //which is stored in 'valKeyDown' at keydown event.
        $(this).val(valKeyDown);                    //It is not possible to check this inside 'integerOnly' function as,
    }                                               //one cannot get the text printed by keydown event 
});                                                 //(that's why, this is checked on keyup)

$('.integerOnly').bind('input propertychange', function(e) {    //if user copy-pastes some character value using mouse
    valKeyUp = this.value;
    if (!new RegExp('^[0-9]*$').test(valKeyUp)) {
        $(this).val(valKeyDown);
    }
});

答案 2 :(得分:0)

如果您在localhost中开发应用程序,那么也请参考这些问题。
"No 'Access-Control-Allow-Origin' header is present on the requested resource"


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