問題描述
我們可以通過 json
攔截器將 JSON 發送到 Struts2 動作.http:///tech.learnerandtutor.com/send-json-object-to-struts-2-action-by-jquery-ajax/.
We can send JSON to Struts2 actions by json
interceptor. There is good sample at http://tech.learnerandtutor.com/send-json-object-to-struts-2-action-by-jquery-ajax/.
json
攔截器要求請求 content type
為 application/json
或 application/json-rpc
這個可以在我們直接使用Ajax的時候設置,比如:
The json
interceptor requires that the requests content type
be application/json
or application/json-rpc
this can be set when we directly use Ajax like:
$.ajax({
url: "writeJSON.action",
data: data1,
dataType: 'json',
contentType: 'application/json',
但我們正在使用 struts 2 jquery 插件標簽(sj:a
和 sj:submit
...)為我們管理 ajax 調用.
But we are using struts 2 jquery plugin tags (sj:a
and sj:submit
...) which manage ajax calls for us.
有什么方法可以用這些標簽設置contentType
.
Is there any way that we can set the contentType
with these tags.
我沒有找到這個標簽屬性.
I did not find tag properties for this.
如果沒有實現,有沒有其他方法可以設置.
If it is not implemented is there any other way to set it.
推薦答案
您應該將 Content-type: application/json
標頭設置為 $.ajax()
.
You should set Content-type: application/json
header to xhr object that is used by the $.ajax()
.
$.ajax({
url: "writeJSON.action",
data: data1,
dataType: 'json',
contentType: 'application/json',
插件使用$.ajaxSubmit()
,這種提交應該被淘汰,因為它不適用于json數據.
The plugin is using $.ajaxSubmit()
, this kind of submission should be eliminated because it doesn't work with json data.
這篇關于使用 strut2 jquery 插件標簽將 JSON 對象發送到 Struts 2 操作的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!