繁体   English   中英

为什么此ajax代码不起作用?

[英]Why is this ajax code not working?

我正在尝试使用下面的代码使用ajax提交表单,但显然似乎不起作用。

有任何解决此问题的建议吗?

$(".dialog-set-new-message").on('keyup', '.ctxtareados', function(b){
    if(b.keyCode == 8){return false;}
    if(b.keyCode == 13){ 
    // 
    $("#submit_new_message_cbox").submit(function(eventmsg){
    $(".loader-wait-gif-con").fadeIn(500);  
    eventmsg.preventDefault();
    $.ajax({
    url: "https://www.mypage.com/success/set_new_cbox_message.php",     // Url to which the request is send
    type: "POST",             // Type of request to be send, called as method
    data:  new FormData(this),    // Data sent to server, a set of key/value pairs representing form fields and values 
    contentType: false,           // The content type used when sending data to the server. Default is: "application/x-www-form-urlencoded"
    cache: false,         // To unable request pages to be cached
    processData:false,        // To send DOMDocument or non processed data file it is set to false (i.e. data should not be in the form of string)
    success: function(data)     // A function to be called if request succeeds
    {
    $(".dialog-new-chat-open-con").load("../success/refresh_new_cbox.php", function(){
    $(".loader-wait-gif-con").fadeOut(500);
    $(".con-sub-nav-chat h3").html("Messages updated.");
    $(".con-sub-nav-chat").css("display", "block");
    $(".con-sub-nav-chat").fadeOut(7000);
    $(".all-msg-new-chat-box").animate({scrollTop: $("#sldum").offset().top}, 1000);
    });
    $('#submit_new_message_cbox')[0].reset();  
    }
    });
    });
    //  
    }
    });

- 固定 -

我在php文件中添加了一个input [submit]并修改了代码,如下所示:

PHP代码添加:

<input style="visibility: hidden;" id="submitnmcbox" type="submit">

修改了JS代码(与上述相同):

$(".dialog-set-new-message").on('keyup', '.ctxtareados', function(b){
if(b.keyCode == 8){return false;}
if(b.keyCode == 13){ 
$("#submitnmcbox").click();
}
});

//Set new msg chat
$("#submit_new_message_cbox").on('submit', function(eventmsg){
$(".loader-wait-gif-con").fadeIn(500);  
eventmsg.preventDefault();
$.ajax({
url: "https://www.dudoby.com/success/set_new_cbox_message.php",     // Url to which the request is send
type: "POST",             // Type of request to be send, called as method
data:  new FormData(this),    // Data sent to server, a set of key/value pairs representing form fields and values 
contentType: false,           // The content type used when sending data to the server. Default is: "application/x-www-form-urlencoded"
cache: false,         // To unable request pages to be cached
processData:false,        // To send DOMDocument or non processed data file it is set to false (i.e. data should not be in the form of string)
success: function(data)     // A function to be called if request succeeds
{
$(".dialog-new-chat-open-con").load("../success/refresh_new_cbox.php", function(){
$(".loader-wait-gif-con").fadeOut(500);
$(".con-sub-nav-chat h3").html("Se han actualizado los mensajes.");
$(".con-sub-nav-chat").css("display", "block");
$(".con-sub-nav-chat").fadeOut(7000);
$(".all-msg-new-chat-box").animate({scrollTop: $("#sldum").offset().top}, 1000);
});
$('#submit_new_message_cbox')[0].reset();  
}
});
});
//  

我认为您错误地使用了FormData()构造函数。 根据MDNFormData()构造函数接受HTML DOM表单元素( 可选 )。 例:

 <form name="myForm"> <input type="text" name="text_field_1" value="test1" /><br /><br /> <input type="text" name="text_field_2" value="test2" /><br /><br /> <input type="text" name="text_field_3" value="test3" /><br /> </form> <script type="text/javascript"> var myForm = document["myForm"]; var formData = new FormData(myForm); console.log(formData.get("text_field_2")); // returns "test2" </script> 

暂无
暂无

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

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