[英]How to return just data instead of html using jquery and javascript?
I have the following javascript code in my app. 我的应用程序中包含以下javascript代码。
$.get("/readers/intotoid/number",function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
For that url, my rails app does: 对于该网址,我的rails应用程序执行以下操作:
def convert
@book = Book.find(1)
params[:integer]
end
When I display the alert, I just want params[:integer] to show. 当显示警报时,我只想显示params [:integer]。 Instad the entire html of the page /readers/intotoid/number displays.
将页面的整个html插入/ readers / intotoid / number显示。 I thought by returning params[:integer] in the convert method, data would be equal to that.
我认为通过在convert方法中返回params [:integer],数据将等于该值。 Any advice on how to do this?
有关如何执行此操作的任何建议?
将其添加到您的convert方法的底部:
render :text => params[:integer], :layout => false
I'm assuming that you want to return a Book as data. 我假设您要返回一本书作为数据。
Your convert method could return as json: 您的convert方法可能返回为json:
def convert
@book = Book.find(1)
respond_to do |format|
format.json { render :json => @book }
end
end
So you need either ask your convert method inside your controller that you want a response as json: 因此,您需要在控制器内询问您的convert方法是否要以json作为响应:
$.get("/readers/intotoid/number.json",function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
我不熟悉Rails,但是应该
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.