【发布时间】:2013-12-29 13:06:45
【问题描述】:
控制器 abcd 的代码 ....................
respond_to :html, :json
def index
respond_to do |format|
format.html
format.json { render :msg=>"comment oK"}
end
javascript代码 ..................................
$(document).ready(function() {
$('#button').click(function(){
$.ajax({
type : 'POST',
dataType: 'script',
url : "http://localhost:3000/abcd/index",
data : { },
processData:false,
error : function(xhr, status) {
},
complete: function(data) {
alert(data.msg);
},
success : function(data) {
window.alert("it worked");
},
});
return false;
});
});
在上面的代码中,当我点击按钮时,只有 alert(data.msg);有效,它会发出警报 Undefined 。为什么会发生,有什么问题?为什么评论 ok 没有在弹出警报中显示??
【问题讨论】:
-
尝试访问数据["msg"]
-
为了得到 json 格式的响应,标准做法是在 url 后面加上
.json。尝试调用http://localhost:3000/abcd/index.json,Rails 会很神奇。 -
data['msg'] 或 localhost:3000/abcd/index.json 都不起作用
标签: jquery ruby-on-rails ajax json