如何用javascript将作用域传递给我的ajax回调函数

How to pass scope to my ajax callback function in javascript

本文关键字:我的 ajax 回调 函数 何用 javascript 作用域      更新时间:2023-09-26

我正在进行第三次下拉。基于第一个下拉列表,我显示第二个下拉列表。根据第二个下拉列表,我显示第三个。所有都是动态下拉菜单。在页面刷新时,我的下拉列表没有值。

我的问题是是否有机会将作用域设置为我的restcall或回调函数。。

这是我的代码,

$(document).ready(function() {
    $("#customerDetails").change(function() {
        var value1 = $('#customerDetails :selected').text();
        $.ajax({
            type : 'POST',
            url : 'environments',
            data : {
                selectedcustomername : value1
            },
            success : function(result) {
                getEnvNames(result);
            }
        });
    });
});

这是我的控制器代码,

@RequestMapping(value = "/environments", method = RequestMethod.POST)
public @ResponseBody String getEnvironmentNames(HttpServletRequest request,HttpServletResponse response,@RequestParam String selectedcustomername) throws SQLException {
    request.setAttribute("selectedcustomername", selectedcustomername);
    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject("environments", new Environments());
    List<Environments>  environmentnamesList= loginDelegate.getEnvironments(selectedcustomername);
    Collections.sort(environmentnamesList, new CustomComparator());
    Gson gson = new Gson();
    System.out.println("gson"+gson);
    String jsonString = gson.toJson(environmentnamesList);
    System.out.println("jsonString"+jsonString);
    return jsonString;
}

期望您通过说scope来表示上下文。你可以稍微改变成功函数来实现这一点,

Success :getEnvNames.bind(contextYouWant),

你的意思是在成功函数中添加一个范围可以使用来完成

with Statement
with(scope){}