[英]Rails Action rendering html instead of json
I am using Rails 3. I really have a strange problem. 我正在使用Rails3。我确实有一个奇怪的问题。 I have a list of Country States as a select box, and I have hooked up
jquery's
$.ajax({})
, on change of the States select box. 我有一个国家(地区)列表作为选择框,并且在状态选择框更改时已将
jquery's
$.ajax({})
连接起来。
The action takes a state code as a parameter and returns the districts that belong to a specific state. 该操作将状态代码作为参数,并返回属于特定状态的地区。 When the request is sent to
/districts?state=1
, it responds with a status code of 302(found)
and then it also sends the current html page as a response. 当请求发送到
/districts?state=1
,它以302(found)
的状态码进行响应,然后还发送当前的html页面作为响应。
Following is my code: 以下是我的代码:
# Controller Action
def districts
state = params[:state]
respond_to do |format|
format.json{ render :json => get_districts(state) }
end
end
# jQUery Request:
$.ajax({
url: '<%= districts_path %>',
data: {state: 1},
success: function(data){
for(i in data){
options += '<option value="'+data[i].code+'">'+data[i].district+'</option>'
}
$("#some_element").html('<select id="some_district" name="some_district">'+options+'</select>')
}
});
Try setting the dataType
option to the ajax call: 尝试将
dataType
选项设置为ajax调用:
$.ajax({
url: '<%= districts_path %>',
dataType: 'json',
data: {state: 1},
HTTP 302, in my experience, is due to authentication issues. 以我的经验,HTTP 302是由于身份验证问题引起的。 You should consider posting your controller code/routes and any authentication as well as any errors that may be occurring.
您应该考虑发布您的控制器代码/路由,任何身份验证以及可能发生的任何错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.