简体   繁体   English

AJAX调用控制器动作

[英]AJAX call to controller action

I have a "mainpage" controller, with a view of the same name, containing a boostrap navbar along the top. 我有一个“主页”控制器,具有相同名称的视图,其顶部包含一个boostrap导航栏。 Upon clicking the various options of the navbar, a div at the bottom of the view has it's contents replaced with an appropriate partial (using a "replaceWith / render" call in a .js.erb file) 单击导航栏的各个选项后,视图底部的div会将其内容替换为适当的部分(在.js.erb文件中使用“ replaceWith / render”调用)

For one such partial, I have a form with 2 select boxes. 对于一个这样的局部,我有一个带有2个选择框的表格。 These need to be populated dynamically on loading of the partial, so I am trying to put some AJAX together. 这些需要在部分加载时动态填充,因此我试图将一些AJAX放在一起。 Below is my AJAX efforts thus far (found at the end of my partial's code): 以下是到目前为止我的AJAX工作(在我的部分代码的末尾找到):

<script>
    // Get Perforce Suites
    var xmlhttp;
    if (window.XMLHttpRequest) // modern browser
    {
        xmlhttp=new XMLHttpRequest();
    }
    else // proper old school! IE 6 and older. Does anyone...? Never mind...
    {
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            //processing of responsetext to go in here
        }

    }
    xmlhttp.open("GET","/perforce_sync/getPerforce", true);
    xmlhttp.send();
    alert (xmlhttp.responseText);
</script>

So you can see I still have the response processing to do... but as it stands, the alert(xmlhttp.responseText) is coming back empty. 因此,您可以看到我仍有响应处理要做...但是就目前而言,alert(xmlhttp.responseText)返回为空。

I'm guessing its the xmlhttp.open that is wrong - I'm trying to call an action (getPerforce) in the "perforce_sync" controller. 我猜想它的xmlhttp.open是错误的-我试图在“ perforce_sync”控制器中调用一个动作(getPerforce)。 I'm not getting any errors in the server output - but nor am I getting anything back from the request! 我在服务器输出中没有收到任何错误-但从请求中也没有得到任何回复!

Note - the above script was based mostly on http://tinyurl.com/ygzdwg with the URL based on what I read here http://tinyurl.com/netps6v 注意-上面的脚本主要基于http://tinyurl.com/ygzdwg,URL基于我在此处阅读的内容http://tinyurl.com/netps6v

Any guidance welcomed! 欢迎任何指导! Thanks! 谢谢!

Let me explain how to use ajax: 让我解释一下如何使用ajax:


Ajax 阿贾克斯

Ajax stands for Asynchronous Javascript And XML - which basically means it uses Javascript to send a request to a server on your behalf Ajax代表异步Javascript和XML-基本上意味着它使用Javascript代表您向服务器发送请求

The JS in your browser can then handle the response, allowing you to manipulate the DOM as required (update page without refresh) 然后,浏览器中的JS可以处理响应,从而允许您根据需要操作DOM(更新页面而不刷新)

Although you can use what you've got, JQuery has made Ajax a lot easier : 尽管您可以使用已有的功能 ,但是JQuery使Ajax更加容易

$.ajax({
   url: "/perforce_sync/getPerforce",
   data: [[form data]]
   success: function(data) { 
       //append data to DOM
   }
});

Calling this will allow you to handle the data you need from your controller without refresh 调用此命令将使您无需刷新即可处理控制器中所需的数据


Code

To give you specific info, you've mentioned that your application needs to populate another select box depending on the value of the first select 为了给您提供特定信息,您已经提到您的应用程序需要根据第一次选择的值填充另一个选择框。

I'd do this: 我会这样做:

#config/routes.rb
scope module: 'perforce_sync' do
   get "getPerforce", to: "controller#action"
end

#app/assets/javascripts/application.js
$("#select1").on("change", function(){ 
    $.ajax({
        url: "/perforce_sync/getPerforce"
        data: $(this).val(),
        dataType: "json",
        success: function(data) {
            //handle data
        },
        error: function(data) {
           alert("error");
        }
    });
}); 

#app/controllers/your_controller.rb
def action
    respond_to do |format|
        format.json
    end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM