javascript - 将数据从 JQuery 方法传递到 AJAX 调用

标签 javascript jquery ajax

如何将“appName”和“评级”从初始点击函数传递到“解析”函数?提前感谢您的帮助。我尝试使用 data: {appName: appName, rating: rating} 但如果这确实有效,我仍然不确定如何在“解析”函数中捕获数据

          <script>
            $(document).ready(function(){
              $('.one, .two, .three, .four, .five').click(function(){

                    //get the app name that's to be rated.
                    var appName = $(this).parents('*:eq(4)').text();
                    appName = appName.split("\n");
                    appName = appName[2]

                    //get the rating given to the app.
                    var rating = $(this).attr('class');

                    if(rating == 'one'){
                        rating = 1;
                    }
                    else if(rating == 'two'){
                        rating = 2;
                    }
                    else if(rating == 'three'){
                        rating = 3;
                    }
                    else if(rating == 'four'){
                        rating = 4;
                    }
                    else if(rating == 'five'){
                        rating = 5;
                    }


                $.ajax({
                    url: 'http://xxxxxx',    
                    dataType: 'xml',  
                    success: parse,
                    error: loadfail
                });

                });
            });
        </script>

        <script>
        function loadfail(){
          alert("Error: Failed to read file!");
        }

        function parse(document){

            $(document).find("ViewAll").find("ROW").each(function(){
                 var optionLabel = $(this).find('SC_DF_FIELD_1').text();
                 var optionValue = $(this).find('SC_DF_FIELD_4').text();
                 alert(optionLabel + ":" + optionValue);
            });
        }
        </script>

最佳答案

您可以将内联函数传递到 success 参数中,其中包含您需要的变量。

$.ajax({
    url: 'http://xxxxxx',    
    dataType: 'xml',  
    success: function(result) { parse(result, appName, rating); },
    error: loadfail
});

function parse(result, appName, rating) {
    // etc...
}

关于javascript - 将数据从 JQuery 方法传递到 AJAX 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17574878/

相关文章:

javascript - 为什么可以删除窗口的重写属性?

javascript - 如何从克隆表中获取数据属性?

javascript - 在 addListener 中传递谷歌事件数据

javascript - 在 summer note JS 中加载数据时弹出意外标识符错误

javascript - 使用 AJAX 根据选择填写表单字段

javascript - 如何删除 window.location 中的部分 URL?

javascript - 不用 ctrl+click 的 Rowunselect

php - 推荐一个不错的非第三方简易聊天室

jquery - 在 Laravel 4.2 中使用 Ajax,一旦用户通过身份验证,登录表单不会重定向到预期页面

javascript - 将 Jenkins 用于基于 Gulp 的项目