2017-10-01 22 views
0

以下のコードでajaxを使用してフォームを送信しようとしていますが、明らかに機能していないようです。なぜこのajaxコードは機能しませんか?

この問題を解決するための提案はありますか?

--FIXED--

$(".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(); 
    } 
    }); 
    }); 
    // 
    } 
    }); 
Iは入力PHPファイルの[提出]を添加し、以下のようにコードを変更し:

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(); 
} 
}); 
}); 
// 
+0

? – pciang

+0

@pciangコンソールには0エラーが表示されます。 –

答えて

0

FormData()コンストラクタを間違って使用していると思います。 MDNによれば、FormData()コンストラクタは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>

+1

しかし、私は他の関数で同じAjaxコードを実装し、完全に動作します。私はそうは思わない。 –

+0

ああ、申し訳ありませんが、 '$ .ajax()'呼び出しが '.submit()'スコープ内にあることを確認できませんでした。 – pciang

+0

同じAjaxコードを実装した他の関数のコードを表示できますか? –

関連する問題