Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当有人单击按钮时,从ajax调用触发successhandler_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 当有人单击按钮时,从ajax调用触发successhandler

Javascript 当有人单击按钮时,从ajax调用触发successhandler,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有一个ajax调用,它从连接到api的php文件中获取数据。我有一个成功处理程序,它从ajax调用接收数据,然后从API库加载函数。问题是,我希望用户单击一个按钮,然后在success调用中运行函数(使用前面ajax调用中的相同数据) 我相信我的ajax调用需要留在window.onload=function()中,如果我试图在用户单击按钮时运行ajax调用,那么它将不起作用 <html> <head> <title>hi</title>

我有一个ajax调用,它从连接到api的php文件中获取数据。我有一个成功处理程序,它从ajax调用接收数据,然后从API库加载函数。问题是,我希望用户单击一个按钮,然后在success调用中运行函数(使用前面ajax调用中的相同数据)

我相信我的ajax调用需要留在window.onload=function()中,如果我试图在用户单击按钮时运行ajax调用,那么它将不起作用

<html>
<head>
    <title>hi</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://api.screenleap.com/js/screenleap.js"></script>
    <script>
            function hello(){
                alert("hi");
            }
            function successHandler(data){
                screenleap.startSharing('DEFAULT', data, {
                    nativeDownloadStarting: hello
                    });
            }

            window.onload = function() {
                $.ajax({
                    type: "POST",
                    url: "key.php",
                    data: '',
                    success: successHandler,
                    dataType: "json"
                }); 
            };


    </script>
</head>
<body>
    <input type="submit" value="submit" id="submit">
</body>
</html>

你好
函数hello(){
警报(“hi”);
}
函数successHandler(数据){
screenleap.startSharing('DEFAULT',数据{
nativeDownloadStarting:你好
});
}
window.onload=函数(){
$.ajax({
类型:“POST”,
url:“key.php”,
数据:“”,
success:successHandler,
数据类型:“json”
}); 
};
请让我知道我如何能做到这一点,任何帮助将不胜感激

更新: 所以我试着调整我的代码,但由于某种原因,当我点击按钮时,什么也没有发生。它甚至没有弹出我的console.log来确认我甚至点击了按钮

<html>
<head>
    <title>hi</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://api.screenleap.com/js/screenleap.js"></script>
    <script>
            var dataFromServer;

            function hello(){
                alert("hi");
            }
            function successHandler(data){
                dataFromServer = data;
                console.log("data from server: ")
                console.log(dataFromServer);
            }

            window.onload = function() {
                $.ajax({
                    type: "POST",
                    url: "key.php",
                    data: '',
                    success: successHandler,
                    dataType: "json"
                }); 
            };

            $("#submit").click(function(){
                console.log('clicked submit');
                if(dataFromServer)
                {   
                    console.log(datafromserver);
                    screenleap.startSharing('DEFAULT', dataFromServer, {
                        nativeDownloadStarting: hello
                    });
                }
            });
    </script>
</head>
<body>
    <input type="submit" value="submit" id="submit">
</body>
</html>

你好
var-dataFromServer;
函数hello(){
警报(“hi”);
}
函数successHandler(数据){
dataFromServer=数据;
log(“来自服务器的数据:”)
console.log(dataFromServer);
}
window.onload=函数(){
$.ajax({
类型:“POST”,
url:“key.php”,
数据:“”,
success:successHandler,
数据类型:“json”
}); 
};
$(“#提交”)。单击(函数(){
log('clicked submit');
if(dataFromServer)
{   
console.log(datafromserver);
screenleap.startSharing('DEFAULT',dataFromServer{
nativeDownloadStarting:你好
});
}
});
祝你一切顺利


--24x7

您可以将数据存储到某个全局变量,然后单击“使用同一变量”按钮

var dataFromServer;

function successHandler(data){
    dataFromServer = data;
}

jQuery("#submit").click(function(){
    if(dataFromServer)
    {   
        screenleap.startSharing('DEFAULT', dataFromServer, {
        nativeDownloadStarting: hello
        });
    }
});
更新:

使用jquery onload函数来填充数据,而不是window.onload


你好,谢谢你回复我!我已经更新了上面的问题,如果我能澄清任何事情,请告诉我。@24x7更新答案。
 $(function() {
            $.ajax({
                type: "GET",
                url: "https://api.github.com/users/mralexgray",
                data: '',
                success: successHandler,
                dataType: "json"
            }); 
        });