Javascript 请求的建议-使用json/jquery&;在函数之间传递变量;AJAX

Javascript 请求的建议-使用json/jquery&;在函数之间传递变量;AJAX,javascript,jquery,ajax,json,frontend,Javascript,Jquery,Ajax,Json,Frontend,我已经看过很多关于SO的“类似”问答帖子,但老实说,由于我对js编程没有太多的把握,我发现很难理解很多答案(只要它们适用于我自己的情况) 上下文是这样的,我有两个php脚本,一个返回一个客户ID列表(json编码),一个返回他们对新闻提要的偏好(json编码) 我写了以下内容,通过谷歌搜索了解了如何在jQuery中设置ajax函数: $('document').ready(function() { $.ajax({ type:'GET', url: 'cust_selec

我已经看过很多关于SO的“类似”问答帖子,但老实说,由于我对js编程没有太多的把握,我发现很难理解很多答案(只要它们适用于我自己的情况)

上下文是这样的,我有两个php脚本,一个返回一个客户ID列表(json编码),一个返回他们对新闻提要的偏好(json编码)

我写了以下内容,通过谷歌搜索了解了如何在jQuery中设置ajax函数:

$('document').ready(function() {
    $.ajax({
        type:'GET', url: 'cust_selection.php', data: '',
        succes:function(cstmrid) {
            var clistlen = cstmrid.length;
            var i=0;
            var cstmr;
            for( ;cstmr=cstmrid[i++]; ) {
            $('#adminPanel>ul>li').append("<a href='' onclick='alert("+cstmr+")' class='lst_admin basic'>"+cstmr+"</a>"); //alert to be replaced with a function call which passes customerid to the function below.
            }
        },
        dataType:'json'
    });

    var cstmrid = "483972258"; //hardcoded for testing purposes
    $.ajax({
        type:'GET', url:'newsfpref.php?', data:'cref='+cstmrid,
        success:function(npfdata) {
            var item;
            var n=0;
            for( ;item=npfdata[n++]; ) {
                var news = npfdata[n].nsource;
                $('#adminMain>table>tbody').append("<tr><td>"+item+"</td></tr>");
            }
         },
         dataType:'json'
    });
});
$('document').ready(函数(){
$.ajax({
类型:'GET',url:'cust_selection.php',数据:“”,
成功:功能(cstmrid){
var clistlen=cstmrid.length;
var i=0;
var-cstmr;
对于(;cstmr=cstmrid[i++];){
$('#adminPanel>ul>li')。追加(“”;//要替换为函数调用的警报,该函数调用将customerid传递给下面的函数。
}
},
数据类型:'json'
});
var cstmrid=“483972258”;//硬编码用于测试目的
$.ajax({
类型:'GET',url:'newsfpref.php',数据:'cref='+cstmrid,
成功:功能(npfdata){
var项目;
var n=0;
对于(;item=npfdata[n++];){
var news=npfdata[n].nsource;
$('#adminMain>table>tbody')。追加(“+item+”);
}
},
数据类型:'json'
});
});
现在,从第一个ajax函数中,我得到了一个链接列表,我希望能够单击它来启动第二个ajax函数,并将客户id传递给它,以便它能够获取他们为页面配置的新闻源列表。 警报和硬编码的客户id都表明功能正在“工作”,但当我尝试调整第一个功能时,会: …

$('#adminPanel>ul>li').append("<a href='' onclick='getCustomerNP("+cstmr+")' class='lst_admin basic'>"+cstmr+"</a>");
function getCustomerNP(cstmrid) {
    $.ajax({
        type:'GET', url:'newsfpref.php?', data:'cref='+cstmrid,
        success:function(nprfdata) {
            var item;
            var n=0;
            for( ;item=npfdata[n++]; ) {
                var news = npfdata[n].nsource;
                $('#adminMain>table>tbody').append("<tr><td>"+item+"</td></tr>");
            }
         },
         dataType:'json'
    });
}
$('adminPanel>ul>li')。附加(“”);
。。。正在调用第二个函数的修改版本,如下所示: …

$('#adminPanel>ul>li').append("<a href='' onclick='getCustomerNP("+cstmr+")' class='lst_admin basic'>"+cstmr+"</a>");
function getCustomerNP(cstmrid) {
    $.ajax({
        type:'GET', url:'newsfpref.php?', data:'cref='+cstmrid,
        success:function(nprfdata) {
            var item;
            var n=0;
            for( ;item=npfdata[n++]; ) {
                var news = npfdata[n].nsource;
                $('#adminMain>table>tbody').append("<tr><td>"+item+"</td></tr>");
            }
         },
         dataType:'json'
    });
}
函数getCustomerNP(cstmrid){ $.ajax({ 类型:'GET',url:'newsfpref.php',数据:'cref='+cstmrid, 成功:函数(nprfdata){ var项目; var n=0; 对于(;item=npfdata[n++];){ var news=npfdata[n].nsource; $('#adminMain>table>tbody')。追加(“+item+”); } }, 数据类型:'json' }); } 在这一点上,一切似乎都失败了。第二个函数似乎没有“接收”变量,我不确定这是否是我忽略的基本内容(比如一些混乱的“和”位置),或者我试图实现的实际上不是jQuery ajax函数彼此交互的方式

正如你所看到的,我已经从很多问答线程中蚕食了一些代码和想法,但是没有太多理解的复制会导致令人沮丧的依赖生活

我希望你能提供尽可能广泛的评论,以及一两个解决方案(当然)

编辑:为了不让任何人进一步困惑,我一直在修改上述内容,并在修改过程中纠正了我(许多)的错误和打字错误。目前,代码如下所示:

$('document').ready(function () {
    $.ajax({
        type: 'GET', url: 'cust_selection.php', data: '',
        succes: function (cstmrid) {
            var clistlen = cstmrid.length;
            var i = 0;
            var cstmr;
            for (; cstmr = cstmrid[i++]; ) {
                var a = $("<a href='' class='lst_admin basic'>" + cstmr + "</a>").click(function () {
                    getCustomerNP(cstmr)
                })
                $('#adminPanel>ul>li').append(a); //alert to be replaced with a function call which passes customerid to the function below.
            }
        },
        dataType: 'json'
    });
    function getCustomerNP(cstmr) {
        alert(cstmr);
    }
});
$('document').ready(函数(){
$.ajax({
键入:“GET”,url:“cust_selection.php”,数据:“”,
成功:功能(cstmrid){
var clistlen=cstmrid.length;
var i=0;
var-cstmr;
对于(;cstmr=cstmrid[i++];){
变量a=$(“”)。单击(函数(){
getCustomerNP(cstmr)
})
$('#adminPanel>ul>li')。附加(a);//要替换为函数调用的警报,该函数调用将customerid传递给下面的函数。
}
},
数据类型:“json”
});
函数getCustomerNP(cstmr){
警报(cstmr);
}
});

您在
getCustomerNP()
中的
$.ajax()成功函数中有一个输入错误。函数声明:

success:function(nprfdata) {
…有一个参数
npfdata
,但是在您使用的函数中
npfdata
(缺少
r

此外,该代码:

        var item;
        var n=0;
        for( ;item=npfdata[n++]; ) {
            var news = npfdata[n].nsource;
            $('#adminMain>table>tbody').append("<tr><td>"+item+"</td></tr>");
        }
…然后在document ready设置中,设置一个委托事件处理程序来捕获这些锚上的单击:

$('#adminPanel>ul>li').on('click', 'a.lst_admin', function() {
    $.ajax({
        type:'GET', url:'newsfpref.php?', data:'cref='+ $(this).attr('data-cstmr'),
        success:function(npfdata) {
            var item,
                n=0,
                // cache the jQuery object rather than reselecting on every iteration
                $table = $('#adminMain>table>tbody'); 
            // increment n only after the current iteration of the loop               
            for( ;item=npfdata[n]; n++) {
                // change to use item
                $table.append("<tr><td>"+item.nsource+"</td></tr>");
            }
         },
         dataType:'json'
    });
});
$('adminPanel>ul>li')。在('click','a.lst\u admin',function()上{
$.ajax({
类型:'GET',url:'newsfpref.php',数据:'cref='+$(this.attr('data-cstmr'),
成功:功能(npfdata){
var项目,
n=0,
//缓存jQuery对象,而不是在每次迭代时重新选择
$table=$('adminMain>table>tbody');
//仅在循环的当前迭代之后增加n
for(;item=npfdata[n];n++){
//更改为使用项目
$table.append(“+item.nsource+”);
}
},
数据类型:'json'
});
});

当您用“
”)追加您的类似项时。单击(函数(){
getCustomerNP(cstmr)
})
$('adminPanel>ul>li')。附加(a)//将警报替换为将customerid传递给以下函数的函数调用。
}
},
数据类型:“json”
});
函数getCustomerNP(cstmrid){
$.ajax({
键入:“GET”,url:“newsfpref.php?”,数据:'cref='+cstmrid,
成功:函数(nprfdata){
var项目;
var n=0;
对于(;item=npfdata[n++];){
var news=npfdata[n].nsource;
$('#adminMain>table>tbody')。追加(“+item+”);
}
},
数据类型:“json”
});
}
});
“像一些混乱的人”和“位置”——就是这样
    $('document').ready(function () {
        $.ajax({
            type: 'GET', url: 'cust_selection.php', data: '',
            succes: function (cstmrid) {
                var clistlen = cstmrid.length;
                var i = 0;
                var cstmr;
                for (; cstmr = cstmrid[i++]; ) {
                    var a = $("<a href='' class='lst_admin basic'>" + cstmr + "</a>").click(function () {
                        getCustomerNP(cstmr)
                    })
                    $('#adminPanel>ul>li').append(a); //alert to be replaced with a function call which passes customerid to the function below.
                }
            },
            dataType: 'json'
        });

        function getCustomerNP(cstmrid) {
            $.ajax({
                type: 'GET', url: 'newsfpref.php?', data: 'cref=' + cstmrid,
                success: function (nprfdata) {
                    var item;
                    var n = 0;
                    for (; item = npfdata[n++]; ) {
                        var news = npfdata[n].nsource;
                        $('#adminMain>table>tbody').append("<tr><td>" + item + "</td></tr>");
                    }
                },
                dataType: 'json'
            });
        }
    });