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

时间:2013-07-10 15:32:52

标签: javascript jquery ajax

如何将“appName”和“rating”从初始点击功能传递到“解析”功能? 提前感谢您的帮助。我试过使用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>

1 个答案:

答案 0 :(得分:3)

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

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

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