Normal pattern for handling Struts 2 Actions in Jquery post/get/ajax calls
I know I am missing something conceptually here and it keeps tripping me up but I will present a use case and would appreciate a best practice response.
$.ajax({
type: "POST",
url: "/myapp/FilterRecord.action",
data: "pageSource=list_edit_add&table=" + table + "&output=" + output + "&selectedIds=" + json_text,
success: function(data) {
document.close();
document.open();
document.write(data);
}
});
In this case, the ajax method of jquery is being called. A Struts 2 action is being performed using the default result 开发者_JAVA百科type, that is Dispatcher Result. Upon Action.SUCCESS, the success function above is entered. The data being passed in is a complete jsp page, head and body both. In the code above, we are sort of manipulating document.write() in a way which it is not necessarily meant to be used for. The aim of the above is to get both the head section and body section. Some other approaches for setting parts of the page which jquery is better set up for are:
document.all[0].innerHTML = data
${'#someRandomSection'}.html(data)
but neither of them capture the full content being passed to us. What then is the proper way to display the result of a DispatcherResult, that is the entire jsp page which is passed back to us? I have some involved javascript going on for this page, and it is not correctly rendering with the approach I presented in the use case above.
first of all the way you are passing the data is incorrect.. it looks like you are passing it as if its a querystring.. you need to change it to follows:
data: {
pageSource:list_edit_add,
table:table,
output:output,
selectedIds: json_text
}
and you need to specify the dataType
dataType: "html".
Can you setup a jsfiddle for your page..
精彩评论