使用ajax

时间:2016-09-25 23:13:34

标签: ajax ruby-on-rails-4

通过ajax发送帖子请求,但收到上述错误,"没有路由匹配[GET]"。

ajax看起来像这样

$.ajax({
   type: 'POST',
   url: '/stage_progress',
   data: {'subject_id' : subject, 'course_id' : course, "chapter_id" : chapter, "stage_id" : stage, "user_id" : user, "session" : "0", "completed" : "true"},
   success: function(){
       console.log('got it');
   }
});

的routes.rb

post '/stage_progress', to: 'stage_progress#track_progress'

如果我运行rake路线,它会显示到此网址的发布路径,但由于某种原因,ajax正在创建一个" get"请求。任何帮助解释为什么以及如何解决它将非常感激。

1 个答案:

答案 0 :(得分:2)

在查看代码时,您将覆盖控制器“stage_progress”的默认索引操作。调用'/ stage_progress'只会路由到默认索引操作(get)。我猜你要么将路线'/ stage_progess'重命名为其他东西,这样才能解决问题。

或者在其他情况下,您仍然需要相同的路线,然后尝试将其写在所有其他路线之上,

  <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Reading Json Data from Json File</title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="scripts/jquery.js"></script>
    <script>

        //When DOM loaded we attach click event to button
        $(document).ready(function () {

            //after button is clicked we download the data
            $('.button').click(function () {
                debugger;
                //start ajax request
                $.ajax({
                    url: "data.json",
                    //force to handle it as text
                    dataType: "text",
                    success: function (data) {

                        //data downloaded so we call parseJSON function
                        //and pass downloaded data
                        var json = $.parseJSON(data);
                        //now json variable contains data in json format
                        //let's display a few items
                        $('#results').html('Plugin name: ' + json.name + '<br />Author: ' + json.author.name);
                    }
                });
            });
        });
    </script>
    <style>
         .button{
            margin:20px;
            font-size:16px;
            font-weight: bold;
            padding:5px 10px;
        }
    </style>
</head>
<body>
<a href="Json file/data.json" target="_blank">Open Json File</a><br />
    <input type="button" value="Get and Parse JSON" class="button" /><br />
    <span id="results"></span>

</body>
</html>
Besides My JSON file named"Data" contains:

    {
    "name": "select2",
    "title": "Select2",
    "description": "Select2 is a jQuery based replacement for select boxes. It supports searching, remote data sets, and infinite scrolling of results.",
    "keywords": [
        "select",
        "autocomplete",
        "typeahead",
        "dropdown",
        "multiselect",
        "tag",
        "tagging"
    ],
    "version": "3.4.2",
    "author": {
        "name": "Igor Vaynberg",
        "url": "https://github.com/ivaynberg"
    },
    "licenses": [
        {
            "type": "Apache",
            "url": "http://www.apache.org/licenses/LICENSE-2.0"
        },
        {
            "type": "GPL v2",
            "url": "http://www.gnu.org/licenses/gpl-2.0.html"
        }
    ],
    "bugs": "https://github.com/ivaynberg/select2/issues",
    "homepage": "http://ivaynberg.github.com/select2",
    "docs": "http://ivaynberg.github.com/select2/",
    "download": "https://github.com/ivaynberg/select2/tags",
    "dependencies": {
        "jquery": ">=1.7.1"
    }
}

因为路由始终在routes.rb文件中从上到下映射。如果在'POST'路线声明之上还有任何其他'GET'路线,它仍然只需要配置第一条路线。