Javascript 如何从jsonp响应中获取值并将该值设置为其他函数中的其他变量?

Javascript 如何从jsonp响应中获取值并将该值设置为其他函数中的其他变量?,javascript,jquery,jquery-mobile,cordova,Javascript,Jquery,Jquery Mobile,Cordova,如何从jsonp响应中获取变量值并在其他脚本函数中使用它? 在第一个函数中,我从jsonp响应中得到一个值。我在第一个函数中将这个值赋给变量(var userId) 我如何获得userId值并在第二个脚本函数中使用它 <script> $(document).on('pageinit', '#login', function () { $(document).on('click', '#submit', function () { if ($('#username').va

如何从jsonp响应中获取变量值并在其他脚本函数中使用它? 在第一个函数中,我从jsonp响应中得到一个值。我在第一个函数中将这个值赋给变量(var userId)

我如何获得userId值并在第二个脚本函数中使用它

<script>
$(document).on('pageinit', '#login', function () {
$(document).on('click', '#submit', function () { 
    if ($('#username').val().length > 0 && $('#password').val().length > 0) {
        console.log($('#check-user').serialize());
        $.ajax({
            url: 'http://localhost/check.php',
            data: $('#check-user').serialize(),
            type: 'POST',
            beforeSend: function () {
            $.mobile.showPageLoadingMsg(true); 
            },
            complete: function () {
            $.mobile.hidePageLoadingMsg(); 
            },
            success: function (result, status, err) {
            if(result.login){
                    $.mobile.changePage( "#output", { transition: "slideup", changeHash: false });
var userID = //// HERE I NEED TO GET USER ID FROM  jsonp response!!!!

            });
            }
            else{
                alert("An error occurred: " + status + "nError: " + err.status);
            }
            },
            error: function (request, error) {
            }
        });
    } else {
        alert('Please fill all necessary fields');
    }
    event.preventDefault(); 
});

var output = $('#output');
var userid = ////HERE I NEED TO SET USER ID!!!!!!!!!!!!
$.ajax({
    url: 'http://localhost/data.php?user='+userid,
    dataType: 'jsonp',
    jsonp: 'jsoncallback',
    timeout: 5000,
    success: function(data, status){
        $.each(data, function(i,item){ 
            var landmark = '<h1>'+item.name+'</h1>'
            + '<p>'+item.lat+'<br>'
            + item.long+'</p>';

            output.append(landmark);
        });
    },
    error: function(){
       output.text('There was an error loading the data.');
    }
});
});
</script>  

$(文档).on('pageinit','#login',函数(){
$(文档)。在('click','#submit',函数(){
如果($('#username').val().length>0&$('#password').val().length>0){
log($('#检查用户').serialize());
$.ajax({
网址:'http://localhost/check.php',
数据:$('#检查用户')。序列化(),
键入:“POST”,
beforeSend:函数(){
$.mobile.showPageLoadingMsg(true);
},
完成:函数(){
$.mobile.hidePageLoadingMsg();
},
成功:功能(结果、状态、错误){
if(result.login){
$.mobile.changePage(“#输出”,{转换:“slideup”,changeHash:false});
var userID=///这里我需要从jsonp响应中获取用户ID!!!!
});
}
否则{
警报(“出现错误:“+status+”错误:“+err.status”);
}
},
错误:函数(请求、错误){
}
});
}否则{
警报(“请填写所有必要字段”);
}
event.preventDefault();
});
变量输出=$(“#输出”);
var userid=///这里我需要设置用户ID!!!!!!!!!!!!
$.ajax({
网址:'http://localhost/data.php?user='+userid,
数据类型:“jsonp”,
jsonp:'jsoncallback',
超时:5000,
成功:功能(数据、状态){
$.each(数据、函数(i、项){
var landmark=''+项目名称+''
+“”+item.lat+“
” +item.long+“

”; 输出。追加(landmark); }); }, 错误:函数(){ text('加载数据时出错'); } }); });
您可以使用js的全局变量分配功能

在任何函数之外声明变量


var用户id;
函数foo(){
//..
}
或者使用窗口对象从函数内部指定全局变量


函数foo(){
window.userID=。。。;
}

更多可用信息

结果上的用户ID在哪里?
<script>
var userId;
function foo(){
  //..
}
</script>
<script>
function foo() {
    window.userID = ...;
}
</script>