从Jquery ajax调用WebService返回布尔值 [英] Return boolean value from Jquery ajax call to WebService

查看:101
本文介绍了从Jquery ajax调用WebService返回布尔值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,其中我已经加载了一个外部js文件,该文件具有以下代码,该文件从加载到用于检查用户权限的模板代码的同一页面中调用。



这是来自外部js文件的代码模板代码

I have a page in which i have loaded an external js file which has the following code which gets called from the same page where it is loaded into sort of a template code for checking user authority.

This is the code from the external js file Template code

function UserAuthorityCheck(){
        var returnData = $.ajax({
        async: true,
        type: "POST",
        url: "http://localhost:80/myApp/MyWebService.asmx" + "/" + "MyUserInRole",
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });

    returnData.then(success, error);
}

function success(response) {
    if (response.d == 'success') {
        return true;
    }
    else {
        return false;
    }
}

function error(response) {
    return false;
}





该页面还包含调用上述代码的代码



The page also contains this code which calls the above code

$(document).ready(function () {
   $(window).load(function () {                
     var response = UserAuthorityCheck();
                
     if (response != undefined || response != null) {
        isUserAuthority(response);
     }

     function isUserAuthority(response) {
       if (response == true) {
          var element = $(document).find('input#btnsearch');
          element.prop('disabled', false);      
       }
       else {
          var element = $(document).find('input#btnsearch');
          element.prop('disabled', true);
       }
     }
   });
 });





如何在成功时从ajax获得返回值。

它始终将undefined返回到var响应变量,因为它是对ajax的异步调用,因此调用代码继续执行它。



请告知任何人。

提前致谢。



How do i get the return value from the ajax on success.
it always returns undefined to the var response variable as it is asynchronous call to ajax so the invoking code continues it execution.

Kindly advise anybody.
Thanks in advance.

推荐答案

.ajax({
async: true
类型: POST
url: http:// localhost:80 / myApp / MyWebService.asmx + / + MyUserInRole
contentType: application / json; charset = utf-8
dataType: json
});

returnData.then(成功,错误);
}

功能成功(响应){
如果(response.d == ' success'){
return true ;
}
其他 {
return ;
}
}

function 错误(响应){
返回 false ;
}
.ajax({ async: true, type: "POST", url: "http://localhost:80/myApp/MyWebService.asmx" + "/" + "MyUserInRole", contentType: "application/json; charset=utf-8", dataType: "json" }); returnData.then(success, error); } function success(response) { if (response.d == 'success') { return true; } else { return false; } } function error(response) { return false; }





该页面还包含调用上述代码的代码



The page also contains this code which calls the above code


document )。ready( function (){
(document).ready(function () {


window )。load( function (){
var response = UserAuthorityCheck();

if (response!= undefined || response!= null ){
isUserAuthority(response) ;
}

function isUserAuthority(response){
if (response == true ){
var element =
(window).load(function () { var response = UserAuthorityCheck(); if (response != undefined || response != null) { isUserAuthority(response); } function isUserAuthority(response) { if (response == true) { var element =


这篇关于从Jquery ajax调用WebService返回布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆