如何在Ajax中绕过GET请求方法的当前页面?

时间:2015-08-17 19:03:05

标签: javascript jquery ruby-on-rails ajax

例如,我目前正在 / users 页面上,当我从该页面发送请求时,它指的是我需要的 / users / conversations / id 只需对话/ ID 。怎么做?

users.js

$('.start-conversation').click(function (e) {
    e.preventDefault();

    var sender_id = $(this).data('sid');
    var recipient_id = $(this).data('rip');

    $.post("/conversations", { sender_id: sender_id, recipient_id: recipient_id }, function (data) {
        chatBox.chatWith(data.conversation_id);
    });
});

chat.js

   $.get("conversations/" + conversation_id, function (data) {
                $('#chatbox_' + conversation_id).html(data);
                $("#chatbox_" + conversation_id + " .chatboxcontent").scrollTop($("#chatbox_" + conversation_id + " .chatboxcontent")[0].scrollHeight);
            }, "html");

enter image description here

1 个答案:

答案 0 :(得分:1)

您正在使用相对路径:"conversations/" + conversation_id,相对路径将附加到当前路径。

您想要使用绝对路径。由于您希望/conversations/id只需将/添加到路径前面

$.get("/conversations/" + conversation_id