【发布时间】:2012-12-03 20:55:01
【问题描述】:
我有一个表单,如果我提交它,那么我希望在同一页面中从我的 json 响应中打印成功消息。
我从 json 响应中获得了许多值,但我只想要在我的 java 操作中定义的名为“消息”的变量。
格式化的json响应是这样的
`{"errmsg":null,"jsonModel":null,"message":"Succussfuyly completed the task","model":
{"createdDate":null,"createrId":null,"id":null,"themeCaption":null,"themeName":null,
"themeScreenshot":null,"updateId":null,"updatedDate":null},"oper":null,"theme":{"createdDate":null,
"createrId":null,"id":null,"themeCaption":null,"themeName":null,"themeScreenshot":null,
"updateId":null,"updatedDate":null},"themeScreenshot":null}`
从这个响应中,我只想在我的 jsp 页面中打印消息变量。 从我下面的 jquery 代码中,所有值都打印在我的 jsp 页面中。但我只想要消息变量。
index.jsp
<script type="text/javascript">
$(document).ready( function() {
$.subscribe('handleJsonResult', function(event,data) {
$('#result').html("<div id='languagesList'> <s:property value="Message"/> </div>"+'' + data.Message + '');
var list = $('#languagesList');
$.each(event.originalEvent.data, function(index, value) {
list.append('<h1>'+value+'</h1>\n');
});
});
});
</script>
</head>
<body>
<img id="indicator" src="${pageContext.request.contextPath}/images/others/ajax-loader.gif" alt="Loading..." style="display:none"/>
<s:form action="updatethemeimageform" method="post" enctype="multipart/form-data" id="remoteform" theme="simple" >
<s:hidden value="%{#parameters.themeid}" name="themId"/>
<s:file name="themeScreenshot" label="Theme Screenshot" />
<sj:a button="true" id="btnsid" buttonIcon="ui-icon-gear" dataType="json" indicator="indicator" onSuccessTopics="handleJsonResult"
formIds="remoteform" targets="result" >Submit This Form</sj:a>
</s:form>
<sj:div id="result" >
Json Result will come here
</sj:div>
`-----------
---------------
private String Message;
public String updateThemesImage(){
setMessage("Succussfuyly completed the task");
return SUCCESS;
}
------------
-----------
With getter & setters`
从我上面的 jquery 函数我得到这样的输出 请帮我解决这个问题
【问题讨论】:
-
您需要在 JSP 技术中使用的任何函数中解析 JSON 响应并输出该变量。
-
@adityamenon 怎么做?这是我的第一个 json 程序。你能解决我的问题吗
-
如果 JSON 来自页面内的资源,为什么它首先作为事件数据中的字符串而不是对象发送?我假设您正在将一个对象转换为 JSON,然后您需要再次将其转换回来,而不是使用原始对象。
-
如果 JSON 来自 jQuery AJAX...只需将
dataType:'json'添加到 AJAX 选项,响应将被转换为 jQuery 核心内的对象 -
@charlietfl 您能否将其发布为我上述问题的答案
标签: json jquery jquery-plugins struts2-jquery