当前位置: 动力学知识库 > 问答 > 编程问答 >

ruby on rails - Best way to return a string from a controller to ajax call

问题描述:

I want to create an action that returns me a single string value:

def delay_calulation()

dr = Dr.find(params[:id])

delay = dr.calc()

respond_to do |format|

format.json { render json: {"value" => delay}}

end

end

but I a "fail" in my ajax call:

$("#btn_delay").on("click", function () {

$.ajax({

type: "GET",

dataType: "script",

url: "/drs/delay_calulation/1"

})

.done(function(response){

console.log(response);

})

.fail(function(response){

console.log(response);

});

});

How can I get a single string, or something like that.

网友答案:

You need to pass id to the controller action while calling through Ajax. You can retrive the string by using the key name that you have mentioned in the controller.

$.ajax({
            url: '/drs/delay_calulation',
            data: {id: (you need to pass the id)},
            type: "get",
            dataType: "json",
            success: function(data){
                       var a = data["delay"]
            },
            failure: function(data){
                var a = data["delay"]
            }
        });
分享给朋友:
您可能感兴趣的文章:
随机阅读: