weixin_33717117 2014-03-15 16:08 采纳率: 0%
浏览 18

学习JQuery和Ajax

I'm trying to learn JQuery and I have a web page where I've created a function named "getUserList". All this does is performs an AJAX call to the server which returns a JSON string that looks like this: {"code":"1","message":["Alan"]} So we have message which contains an array of one item, in this case.

The function looks like this:

$.fn.getUserList = function(){
  //Call the server and get the list of users currently logged in
  var list;
  $.ajax({
    type: "GET",
    url: "/ChatEngine/ChatServlet/users/list?roomId=" + roomID, 
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
      if (msg.code=="1")
      {
        console.log(msg.message);
        list = msg.message;                         
      }
      else
      {
         alert("Unable to obtain user list. code: " + msg.code + " message: " + msg.message);
      } 
    },
    error: function(err) {
      alert('Get User List Error:' + err.responseText + '  Status: ' + err.status); 
    }
  });
  return list;      
}

In another function I make the call to getUserList() like so:

var list = $(document).getUserList();<br>
for(var i = 0; i < list.length; i++) { ... }<br>

But I keep getting an error saying that list is undefined. As you can see from the function that I am returning the array and according to FireBug, the console is recording the existence of the array: enter image description here

So, why is list undefined? One other question...in order to make a function call I have to keep using the syntax: $(document).somefunctionname(); Why is it I can't just write: somefunctionname(); Why do I always need to prepend the call with $(document)? If I don't, I keep getting an error saying that the function doesn't exist, which isn't true.

Please advise.

Alan

  • 写回答

1条回答 默认 最新

  • weixin_33744854 2014-03-15 16:19
    关注

    Ok, your questions:

    List is undefined because as @A.Wolf and @mamdouh points out, your call is asynchronous, that means, the program doesn't wait for the AJAX call. So, if you would want your function to wait for the result, you should write async: false; this way your AJAX call will be resolved before throwing the answer. Check https://api.jquery.com/jQuery.ajax/ for more info.

    About the second question, you could just write:

    <script>
    function hello() {
       console.log('Hello');
    }
    
    hello();
    </script>
    

    And go. The problem is that when working with jQuery you're not extending the jQuery core if you don't use $.fn.nameFunction().

    Also, check this SO question for extended info, not asked for you but related: jquery - difference between $.functionName and $.fn.FunctionName

    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系