在rails中呈现json后如何在javascript中访问json
如果我在Ruby中呈现json,如何访问javascript中的值 在ruby中,如果我写:在rails中呈现json后如何在javascript中访问json,javascript,ruby-on-rails,ruby,json,Javascript,Ruby On Rails,Ruby,Json,如果我在Ruby中呈现json,如何访问javascript中的值 在ruby中,如果我写: response = {:key => "val"} response = response.to_json render :json => response, :status => 200 如何在javascript中访问“val” 如果我在javascript中使用alert(response),我会看到json周围有一个标记,这会有什么不同吗?或者这是预期的吗 我尝试了jQue
response = {:key => "val"}
response = response.to_json
render :json => response, :status => 200
如何在javascript中访问“val”
如果我在javascript中使用alert(response),我会看到json周围有一个标记,这会有什么不同吗?或者这是预期的吗
我尝试了jQuery.parseJSON(response),但出现了语法错误。如果我试图直接访问响应,我没有得到正确的值-应该
response.key==“val”
评估为真
我是不是在Ruby中设置不正确,还是在javascript中访问不正确,或者两者兼而有之?如果您能显示您的javascript代码,这将非常有帮助。
无论如何,一种方法是使用jQuery的ajax函数并将数据类型设置为json
$.ajax({
type: "GET",
url: "<your link>",
dataType: "json",
success: function(response) {
if (response.key)
alert(response.key);
});
});
$.ajax({
键入:“获取”,
url:“”,
数据类型:“json”,
成功:功能(响应){
if(response.key)
警报(response.key);
});
});
希望这能有所帮助。如果您能显示您的javascript代码,这将非常有帮助。
无论如何,一种方法是使用jQuery的ajax函数并将数据类型设置为json
$.ajax({
type: "GET",
url: "<your link>",
dataType: "json",
success: function(response) {
if (response.key)
alert(response.key);
});
});
$.ajax({
键入:“获取”,
url:“”,
数据类型:“json”,
成功:功能(响应){
if(response.key)
警报(response.key);
});
});
希望这能有所帮助。这里有一个简单的例子 在./config/routes.rb中
match '/index' => 'application#index'
match '/json' => 'application#json'
控制器,./app/controllers/application\u controller.rb:
class ApplicationController < ActionController::Base
protect_from_forgery
def index
# server side code required by index
end
def json
response = {:key => "val"}
response = response.to_json
render :json => response, :status => 200
end
end
class ApplicationController“val”}
response=response.to_json
render:json=>response,:status=>200
结束
结束
ajax请求来自erb页面,在本例中。/app/views/application/index.html.erb:
<script type="text/javascript" charset="utf-8">
$(document).ready(
function(){
$("a#my_ajax").bind("ajax:success",
function(evt, data, status, xhr){
console.log(data.key);
console.log(data.key === "val");
}).bind("ajax:error", function(evt, data, status, xhr){
console.log("doh!")
});
});
</script>
<%= link_to "test", {:controller=>"application", :action => 'json'}, :remote=> true, :id => "my_ajax" %>
$(文件)。准备好了吗(
函数(){
$(“我的ajax”).bind(“ajax:success”,
功能(evt、数据、状态、xhr){
console.log(data.key);
console.log(data.key==“val”);
}).bind(“ajax:error”,函数(evt、数据、状态、xhr){
log(“doh!”)
});
});
“应用程序”,:action=>json'},:remote=>true,:id=>“my_ajax”%>
下面是一个简单的例子
在./config/routes.rb中
match '/index' => 'application#index'
match '/json' => 'application#json'
控制器,./app/controllers/application\u controller.rb:
class ApplicationController < ActionController::Base
protect_from_forgery
def index
# server side code required by index
end
def json
response = {:key => "val"}
response = response.to_json
render :json => response, :status => 200
end
end
class ApplicationController“val”}
response=response.to_json
render:json=>response,:status=>200
结束
结束
ajax请求来自erb页面,在本例中。/app/views/application/index.html.erb:
<script type="text/javascript" charset="utf-8">
$(document).ready(
function(){
$("a#my_ajax").bind("ajax:success",
function(evt, data, status, xhr){
console.log(data.key);
console.log(data.key === "val");
}).bind("ajax:error", function(evt, data, status, xhr){
console.log("doh!")
});
});
</script>
<%= link_to "test", {:controller=>"application", :action => 'json'}, :remote=> true, :id => "my_ajax" %>
$(文件)。准备好了吗(
函数(){
$(“我的ajax”).bind(“ajax:success”,
功能(evt、数据、状态、xhr){
console.log(data.key);
console.log(data.key==“val”);
}).bind(“ajax:error”,函数(evt、数据、状态、xhr){
log(“doh!”)
});
});
“应用程序”,:action=>json'},:remote=>true,:id=>“my_ajax”%>
请显示javascript代码。它基本上只是if(response.key==“val”){…},但它的计算结果为false,我不知道为什么。我想这可能是因为标签的缘故,但这可能只是在使用alert()时发生的。我不确定我是否需要解析JSON,或者它是否已经是JSON。响应应该在ajax回调函数中得到,你做了什么?在该函数中,我做的第一件事是if语句,它没有返回正确的值。请显示javascript代码。基本上只是如果(response.key==“val”){…},但它的评估结果是错误的,我不知道为什么。我想这可能是因为标签的缘故,但这可能只是在使用alert()时发生的。我不确定是否需要解析JSON,或者它是否已经是JSON。响应应该在ajax回调函数中得到,您做了什么?在该函数中,我做的第一件事是if语句,它没有返回正确的值。