简体   繁体   English

在本身是AJAX响应的视图上使用ajaxForm插件

[英]Using ajaxForm plugin on view that is itself an AJAX response

I am building a messaging system for my site. 我正在为我的站点构建消息传递系统。 The mailbox is flat, ie accessing the inbox, or sending a new message does not move to another page, it just toggles divs. 邮箱是平坦的,即访问收件箱或发送新邮件不会移至另一页,它只是切换div。 When a user clicks a message, an AJAX call replaces the inbox div with the chosen thread. 当用户单击一条消息时,AJAX调用将用所选线程替换收件箱div。 Inside the thread view, there is a form to reply to the message. 在线程视图内,有一个表单来回复消息。

A few problems: 一些问题:

  1. From inside this thread_view, which sends an AJAX response to a div nested inside the entire mailbox div, I don't have access to document objects outside of it. 从这个thread_view内部,它向嵌套在整个邮箱div中的div发送AJAX响应,我无权访问其外部的文档对象。 So, I can't manipulate divs outside of this view, such as the one that receives the AJAX beforeSend and Success messages. 因此,我无法在此视图之外操作div,例如接收AJAX beforeSend和Success消息的div。 I think this may be accomplished with some kind of .load(), though I'm not sure exactly how. 我认为这可以通过某种.load()完成,尽管我不确定具体如何。

  2. My AJAX doesn't fire. 我的AJAX不会触发。 I am using the Ajax.Form() plugin. 我正在使用Ajax.Form()插件。 I think this problem might be related to the first, but I can't say for certain. 我认为这个问题可能与第一个问题有关,但我不能肯定地说。 I'm not sure how to begin troubleshooting the Ajax request because I get no errors in the console. 我不确定如何开始对Ajax请求进行故障排除,因为控制台中没有错误。

  3. I wonder if the problem has to do with the fact that I am trying to send an ajaxRequest from a view that is itself a response from a previous ajaxRequest, ie the entire view for the thread is a result of the following, in the same js file as the next request: 我想知道问题是否与以下事实有关:我试图从本身是来自先前ajaxRequest的响应的视图发送ajaxRequest,即该线程的整个视图是同一js中以下结果的结果文件作为下一个请求:

     // compose a message function $('#send_message').on("click", function(e) { var send_message_options = { type: 'post', url: "/users/new_message", beforeSend: function() { //Display a loading message while waiting for the ajax call to complete $('#message').html("Sending message..."); }, // Hide form and display results success: function(response) { $('#message').html(response); } }; $('#message_form').ajaxForm(send_message_options); }); 

My new AJAX request, which does nothing: 我的新AJAX请求,什么都不做:

$('#reply_in_thread').on("submit", function(e) {
e.preventDefault();
console.log("trying for reply");

var reply_options = {
    type: 'post',
    url: "/users/reply",
    beforeSend: function() {
        //Display a loading message while waiting for the ajax call to complete
        $('#reply_message').html("Sending message...");
    },
    // Hide form and display results
    success: function(response) {
        $('#reply_message').html(response);
    }
};

$('#reply_in_thread').ajaxForm(reply_options);

});

I couldn't say why the ajaxForm() plugin failed, but a jquery $.post was successful. 我不能说为什么ajaxForm()插件失败,但是jquery $ .post成功了。 The code that worked below: 起作用的代码如下:

$('#reply_in_thread').on("submit", function(e) {
e.preventDefault();   

var data = $(this).serialize();

$.post('/users/reply',data,function(response){
    $('#reply_message').html(response);
})

});

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

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