简体   繁体   English

如何使用jQuery和JavaScript仅返回数据而不是HTML?

[英]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.

相关问题 为什么我使用 JQuery 得到的只是文本而不是 HTML? - Why i'm getting just text instead of HTML using JQuery? 如何返回到我刚才在HTML5和JavaScript中点击的位置? - How to return to the position I clicked just now in HTML5 and JavaScript? 如何使用 html css javascript/jquery 回复聊天中的特定消息,就像 Skype/whatsapp - how to reply to a specific message in chat just like skype/ whatsapp using html css javascript/jquery 如何在不使用任何mysql查询,仅使用简单的javascript或jquery的情况下在HTML表中进行搜索? - How can I search in a HTML table without using any mysql queries, just a plain javascript or jquery to be specific? jQuery生成错误的html字符串'=“而不是” - jQuery generates wrong html string '=“ instead of just ” Javascript-如何使用.indexOf而不是开头来查找特定的字符串 - Javascript - How to find specific string using .indexOf instead of just the beginning 如何使用jquery或javascript以HTML形式显示本地存储数据? - How to show localstorage data in HTML form using jquery or javascript? 如何使用javascript或jquery获取HTML表格单元格数据 - How to get HTML table cell data using javascript or jquery 如何使jQuery JavaScript库仅影响HTML中的特定div? - How to make a jquery javascript library affect just a specific div in HTML? 如何仅使用javascript和HTML通过链接传递数据? - How can I pass data through a link using just javascript and HTML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM