简体   繁体   English

用于警告ajax调用响应的“未定义”,为什么?

[英]“undefined” for alerting an ajax call response , why?

i am trying to alert the return value captured by the ajax request. 我试图提醒由ajax请求捕获的返回值。 The web method it is refering is returning a boolean value of true or false, and when i am trying to access it outside the ajax method it gives a message "undefined" :? 它所引用的Web方法返回一个布尔值true或false,当我尝试在ajax方法之外访问它时,它给出一条消息“ undefined” :?

       <script type="text/javascript" language="javascript">

       $(document).ready(function () {
           var authInfo;

           $.ajax({
               type: "POST",
               url: "service.asmx/getAuthInfo",
               data: "{}",
               contentType: "application/json; charset=utf-8",
               dataType: "json",
               success: function (msg) {
                   authInfo = msg.d;

               },


           });

           alert(authInfo);          

       });

    </script>

why is the alert(authInfo) giving me as "undefined" ?? 为什么alert(authInfo)给我的是“未定义”? Please help ! 请帮忙 !

where does this piece of code fit on the above context ? 这段代码在上述上下文中适合什么位置?

if(auhthInfo){

                        $(".ansitem").editable('FetchUpdate.aspx', {

                    style: 'background-color:inherit;',
                    type: 'textarea', 
                    indicator: '<img src="spinner.gif">',
                    event: 'dblclick',
                    onblur: 'submit', 
                    submitdata: function (value, settings) { 
                        return { orgval: value};
                        },

                   });


                   };

Your "$.ajax()" call is asynchronous , and thus the results are not available until the response is returned from the server. 您的“ $ .ajax()”调用是异步的 ,因此直到从服务器返回响应后,结果才可用。

If you put your "alert()" inside the "success" callback, it should work (if the HTTP transaction works). 如果将“ alert()”放入“成功”回调中,则它应该起作用(如果HTTP事务起作用)。

EDITED TO HANDLE YOUR NEW CODE: 编辑以处理您的新代码:

<script type="text/javascript" language="javascript">
   $(document).ready(function () {
       var authInfo;
       $.ajax({
           type: "POST",
           url: "service.asmx/getAuthInfo",
           data: "{}",
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           success: function (msg) {
               // alert(msg.d); // here it will work as it is only called when it succeeds.
               MyHanlder(msg);
           },
       });
       // alert(authInfo); // here authinfo has no value as the AJAX call may not have returned.
   });

   function MyHandler(msg) {
      if(msg.d){
           $(".ansitem").editable('FetchUpdate.aspx', {
               style: 'background-color:inherit;',
               type: 'textarea', 
               indicator: '<img src="spinner.gif">',
               event: 'dblclick',
               onblur: 'submit', 
               submitdata: function (value, settings) { return { orgval: value}; },
            });
       };
    }
</script>

It appears: 它出现:

alert(authInfo);

Is running immediately when your document is ready. 准备好文档后立即运行。

However, the variable is not being initialized until after the AJAX call completes. 但是,直到AJAX调用完成后,变量才被初始化。

Try moving the alert to: 尝试将警报移至:

$.ajax({
   type: "POST",
   url: "service.asmx/getAuthInfo",
   data: "{}",
   contentType: "application/json; charset=utf-8",
   dataType: "json",
   success: function (msg) {
       authInfo = msg.d;
       alert(authInfo);
   },
});

If you need to do anything more complex with the value, you can try re-factoring the code into another function: 如果您需要对值进行任何更复杂的操作,则可以尝试将代码重构为另一个函数:

function onSuccess(msg)
{
   if(msg.d)
   {
      window.alert('The value is true!');
   }
   else
   {
      window.alert('The value is false!')
   }
}

$.ajax({
   type: "POST",
   url: "service.asmx/getAuthInfo",
   data: "{}",
   contentType: "application/json; charset=utf-8",
   dataType: "json",
   success: onSuccess,
});

The request and the response that you get via ajax calls are asynchronous . 通过ajax调用获得的请求和响应是asynchronous I think it is not possible to alert the value that you get in response at the end of ready function because you are not assured that you will be getting your response before the function completes execution. 我认为不可能在ready函数结束时提醒您获得的响应值,因为您不确定在函数完成执行之前会得到响应。

You are alerting the authInfo variable before the actual ajax call have returned. 在实际的ajax调用返回之前,您正在警告authInfo变量。 If you try this: 如果您尝试这样做:

success:function(msg) {
   authInfo = msg.d;
   alert(authInfo);
}

I think you will get the correct result. 我认为您会得到正确的结果。

Because the AJAX call is done asynchronously, the alert does not have the value at the time of execution. 由于AJAX调用是异步完成的,因此警报在执行时没有值。 If you place the alert inside the success function you should see the appropriate results. 如果将警报放置在成功功能内,则应该看到适当的结果。 I also noticed you have an extra comma in the $.ajax parameters after the success function parameter. 我还注意到,在成功函数参数之后,$。ajax参数中还有一个逗号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM