我想在我的视图中添加从我的控制器返回的'msg'数据
在我的控制器中我有:
render json: { notice: 'Reply sent.', from: @message.from, msg: @message.content }
在我看来,我有
<script>
$('.new_short_message').bind('ajax:success', function() {
$( this ).before( '<p>message</p>' );
});
</script>
如何引用'msg'数据代替'<p>message</p>'
答案 0 :(得分:1)
ajax:success
处理程序的回调函数有三个参数(xhr
,data
和status
),其中data
包含传输的数据。它是一个普通的JSON对象,因此您可以使用data.notice
,data.from
等直接访问内容。例如:
$('.new_short_message').bind('ajax:success', function(xhr, data, status) {
$(this).before('<p>'+data.msg+'</p>');
});