简体   繁体   English

如何在jquery ajax成功方法中调用spring controller

[英]how to call spring controller in jquery ajax success method

I have created a spring controller and a jsp page. 我创建了一个弹簧控制器和一个jsp页面。 In jsp page I am using jquery ajax call to hit the controller. 在jsp页面中,我使用jquery ajax调用来命中控制器。 Now, this controller returns a json response as string. 现在,此控制器以字符串形式返回json响应。 Now on based of json response in success method, I want to call a next controller call which will return a ModelAndView jsp page. 现在基于json响应成功方法,我想调用下一个控制器调用,它将返回一个ModelAndView jsp页面。 How can I do this. 我怎样才能做到这一点。 Below is my code: 以下是我的代码:

JSP Jquery ajax call: JSP Jquery ajax调用:

$(document).ready(function(){
    $("#submitButton").click(function(e){
         var formData = getFormData();
         if(formData!=false){
         $.ajax({
            type: 'POST', 
            'url': 'http://localhost:8080/Test_ReportingUI/fieldMappingNext.htm',
            data: {jsonData: JSON.stringify(formData)},
            dataType: 'json',
            success: function(response){ 
                 try{
                    var strResponse=jQuery.parseJSON(response);
                }catch(err){}
                if(response.status=='ok')
                {
                    alert ("okokokokokokokokok");
                //I am successfully reaching till here. 
                //But in case of this alert box I want to call a 
                //controller which will return ModelAndView and 
                //should open a corresponding ModelAndView jsp page.
                //something like:
                /*
                $.ajax({
                type: 'GET', 
                'url': 'http://localhost:8080/Test_ReportingUI/abcxyz.htm',  
                )};
                */
                }
                else
                {
                    alert("ERROR!!");
                } 

            },
            timeout: 10000,
            error: function(xhr, status, err){ 
                if(response.status=='timeout')
                {   
                    alert('Request time has been out','');
                }
                console.log(status,err); 
            }
        }); }
     });
});

Controller class methods: 控制器类方法:

@RequestMapping (value="fieldMappingNext.htm", method=RequestMethod.POST)
@ResponseBody String addFieldMappingNext(@RequestParam String jsonData)
{
    String customerID =null;
    String objectID = null;
    String syncFieldName = null;
    String optMapping = null;
    JSONObject jsonResponse = new JSONObject();
    try{
        JSONObject requestedJSONObject = new JSONObject(jsonData);
        customerID = requestedJSONObject.getString("customerID");
        objectID = requestedJSONObject.getString("objectID");
        syncFieldName = requestedJSONObject.getString("syncFieldName");
        optMapping = requestedJSONObject.getString("optMapping");
    }catch(Exception exex){
        exex.printStackTrace();
    }
    if(optMapping.equalsIgnoreCase("direct")){
        long metadataID=rwCustomerService.getMetaDataID(customerID,objectID);
        List<RWFieldDetail> list=rwCustomerService.getFieldDetailNames(metadataID);
        request.setAttribute("customerID", customerID);
        request.setAttribute("objectID", objectID);
        request.setAttribute("optMapping", optMapping);
        request.setAttribute("syncFieldName", syncFieldName);
        request.setAttribute("fieldNames", list);
        try {
            jsonResponse.put("status", "ok");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return jsonResponse.toString();
}

Second Controller method that I want to call from jquery success method: 我想从jquery成功方法调用的第二个Controller方法:

@RequestMapping (value="abcxyz.htm", method=RequestMethod.GET)
ModelAndView fieldMapping(){
ModelAndView modelAndView=new ModelAndView("FieldMappingMainScreenNext");
return modelAndView;
}

How do I do this. 我该怎么做呢。

Since the second handler method returns ModelAndView , you should redirect from the success callback: 由于第二个处理程序方法返回ModelAndView ,因此您应该从成功回调重定向:

...

success: function(response) {
    window.location.replace(response.url);
}

...

In your Java code you can use something like: 在您的Java代码中,您可以使用以下内容:

Map<String, String> map = new HashMap<String, String>();
if(condition1){
   map.put("url","url1.html");
}
if(condition2){
   map.put("url","url2.html");
}

Convert it to a JSON string and revert it back. 将其转换为JSON字符串并将其还原。 Afterwards, in the jquery portion you'll get the response: 然后,在jquery部分中,您将得到响应:

success:function(jsonStr){
    var obj = JSON.parse(jsonStr);
    var url = obj.url;
}

That is how you can get the url. 这就是你如何获得网址。 If you want to load an other page or create an ajax call then you can do it. 如果要加载其他页面或创建ajax调用,则可以执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM