简体   繁体   English

在JS中使用几个函数和回调

[英]Using several functions and callbacks in JS

I have this code that loads on document.ready and it checks if the user is logged in or not: 我有这段代码加载到document.ready上,它检查用户是否登录:

//Checking if the user is logged in or not
$(function(){
$.getJSON("inc/API.php", {command : "getUserName"},
    function(result){
        if(result==null){
            $("#divGreeting").html("Hello, guest!");
        }
        else {
            $("#divGreeting").html("Hello, "+result+"!");
            $("#divHeader").html("Hello, "+result+"! <a href='javascript:logout()'>Logout</a>");
        }
    });

}); });

If he's not - it says "Hello, guest"; 如果不是,则显示“您好,客人”; if he is - it says "Hello, username ", where username is the result in this function. 如果是,则显示“ Hello, username ”,其中username是此函数的result What I need, is to make the username in this part: $("#divGreeting").html("Hello, "+result+"!"); 我需要的是在此部分中输入用户名: $("#divGreeting").html("Hello, "+result+"!"); to be a link, which leads to another page with userID, which I don't have. 成为一个链接,该链接会指向另一个没有用户ID的页面。 Like this: $("#divGreeting").html("Hello, <a href='userpage.html?userid="+HAVE TO GET THE ID HERE+"'>"+result+"</a>!"); 像这样: $("#divGreeting").html("Hello, <a href='userpage.html?userid="+HAVE TO GET THE ID HERE+"'>"+result+"</a>!");

I tried to use this function (it does get the: 我尝试使用此功能(它确实得到了:

function getUserID(){
    $.getJSON("inc/API.php",
    {
        command : "getUserID"
    },
    function(result){
        return result;
    });
}

and to call it like that: $("#divGreeting").html("Hello, <a href='userpage.html?userid="+getUserID()+"'>"+result+"</a>!"); 并这样称呼它: $("#divGreeting").html("Hello, <a href='userpage.html?userid="+getUserID()+"'>"+result+"</a>!"); , but that didn't work, I didn't get any result, except for undefined (I understand it has something to do with asynchronous AJAX). ,但这没有用,除了undefined之外,我没有得到任何结果(我知道它与异步AJAX有关)。

This is the content of API.php: 这是API.php的内容:

case "getUserID":
    echo getUserID();
    break;

it call's for function getUserID() from file BusinessLogic.php, which has this function: 它从文件BusinessLogic.php调用函数getUserID() ,该函数具有以下功能:

function getUserID()
{
$arr = select("SELECT userID FROM users WHERE username='".$_SESSION["username"]."'");
return $arr[0]["userID"];
}

I've been told that I have to use callbacks, but I have no idea how to do that, how to write this callback. 有人告诉我必须使用回调,但是我不知道该怎么做,如何编写此回调。 I'm lost here... 我在这里迷路了...

Some help, please? 请帮忙吗?

$.getJSON("inc/API.php", {command : "getUserID"}, function(result){
    var id = result;
    $.getJSON("inc/API.php", {command : "getUserName"}, function(result){
         $("#divGreeting").html("Hello, "+result+"!");
         $("#divHeader").html("Hello, <a href='userpage.html?userid="+id+"'>"+result+"</a>!");
    });
});

The code that uses your ajax response 使用ajax响应的代码

$("#divGreeting").html("Hello, <a href='userpage.html?userid="+getUserID()+"'>"+result+"</a>!");

Must be in the callback of the ajax function. 必须在ajax函数的回调中。

AJAX queries don't block the execution of a function, so when you call getUserID all it does it initialize the AJAX call and return undefined as no return was set. AJAX查询不会阻止函数的执行,因此,当您调用getUserID时,它都会初始化AJAX调用并返回undefined,因为未设置return。 Adding return like suggested won't work either as all you will do is return something else, jQuery probably. 像建议的那样添加return也不起作用,因为您要做的就是返回其他内容,可能是jQuery。

You have to fetch the id before you can generate anything. 您必须先获取ID,然后才能生成任何内容。 So when you ask for the username and you get one instead of displaying thing straight away you do your AJAX call to get the id and then the callback of that render your HTML. 因此,当您要求提供用户名时,会得到一个而不是立即显示用户名,而是进行AJAX调用以获取ID,然后对该URL进行回调以呈现HTML。

You are already using callbacks. 您已经在使用回调。

The $.getJSON syntax is: $ .getJSON语法为:

jQuery.getJSON( url [, data] [callback] )
  • You have 5 { brackets in your code but only 4 } brackets. 您的代码中有5个{括号,但只有4 }括号。 Maybe this is the error? 也许这是错误?
  • What do you see if you type yourdomain.com/INC/API.php?command=getUserName in your browser? 如果在浏览器中键入yourdomain.com/INC/API.php?command=getUserName ,会看到什么? If the document is empty, the error is in your PHP file, if the username appears, the problem is in your jQuery code. 如果文档为空,则错误在您的PHP文件中,如果出现用户名,则问题在您的jQuery代码中。
  • You didn't tell us what happens if you execute the javascript code. 您没有告诉我们执行JavaScript代码会发生什么情况。 What does appear? 会出现什么? "Hello username", "Hello guest" or nothing? “你好用户名”,“你好客人”还是什么?

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

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