假设所有形式的应用程序具有这样的结构:AJAX(jQuery的)响应
<div id="result_messages"></div>
<form action="/action">
<!-- all the form -->
</form>
用于这种形式提交按钮看起来是这样的:
<input type="button" onclick="$.post('/action', $(form).serialize(), function (data) {
$('#result_messages').html(data); // At this point the 'data' is an standard HTML with a message
});" />
可是,却并不总是响应消息......当数据信息或不如何检测??????:
<input type="button" onclick="$.post('/action', $(form).serialize(), function (data) {
if (isMessage(data))
$('#result_messages').html(data);
else
doActionWith(data);
});" />
使用JSON可能的解决方案:
{ response_type : 'message', data: 'all_data_here' }
{ response_type : 'nomessage', data: 'all_data_here' }
其他的解决办法是把一个特殊的字符串在开始的数据:
<!--message--><ul><li>form was processed</li></ul>
你有没有其他的想法?你对这个解决方案有什么看法?
你是什么意思«不总是消息»?那是什么,其他时间呢? – 2010-10-31 09:31:08