###前提・実現したいこと
Java + JQuery(Ajax) でシングルページアプリを作成中です。
###発生している問題・エラーメッセージ
Ajaxでサーバー側にデータをPOST後、taccountid=2 という変数の値が空になってしまいます。
(正常URL)
http://localhost/api/proposal?t_account_id=2
(異常URL)
http://localhost/api/proposal?t_account_id=
###ソースコード js
Javascript
1$('#proposal').on('click', function() { 2 3 var username = $(':hidden[name="username"]').val(), 4 userid = $(':hidden[name="userid"]').val(), 5 taccountid = $(':hidden[name="taccountid"]').val(), //// この時点では正常な 2 を取得している 6 page = "proposal", 7 urlget = "http://localhost/api/proposal?t_account_id=" + taccountid, 8 urlpost = "http://localhost/api/proposal"; 9 10 $('.content-header, div.row, .col-md-12, .box-info, .box').css( 'display', 'none'); 11 12 $('.content').hide().load('proposal.html').fadeIn(function() { 13 getData(urlget, page); //// この時点でも正常な 2 が入っている 14 $(document).on('submit', '#proposal_form', function(event) { 15 event.preventDefault(); 16 var val1 = $('#proposal_form [name=theme]').val(), 17 val2 = $('#proposal_form [name=content]').val(), 18 data = 19 {"tAccountId":taccountid, "id":null, "theme":val1, "content":val2, "status":"\<span class=\"label label-warning\"\>\<i class=\"fa fa-envelope\"\>\<\/i\> 未読\<\/span\>"}; 20 callModal(urlget, urlpost, data, page); //// この POST後に('#proposal')をクリックするとtaccountidはすでに空になっている。関数は後述 21 }); 22 console.log("taccountid_check1: "+taccountid); 23 }); 24});
###ソースコード callModal.js
Javascript
1function callModal(urlget, urlpost, data, page) { 2 3 var color, 4 token = $("meta[name='_csrf']").attr("content"), 5 header = $("meta[name='_csrf_header']").attr("content"); 6 7 switch (page){ 8 case "proposal": 9 color = "#00B3EA"; 10 break; 11 } 12 13 swal({ 14 title: "送信します。よろしいですか?", 15 type: "warning", 16 showCancelButton: true, 17 confirmButtonColor: color, 18 confirmButtonText: "送信する", 19 cancelButtonText: "戻る", 20 animation: false, 21 closeOnConfirm: false 22 }, 23 function (){ 24 $.ajax({ 25 type : 'POST', 26 url : urlpost, 27 data : JSON.stringify(data), 28 contentType: 'application/json', 29 dataType : 'JSON', 30 scriptCharset : 'utf-8', 31 timeout: 10000, 32 beforeSend: function(xhr) { 33 xhr.setRequestHeader(header, token); 34 } 35 }) 36 .done(function(response){ 37 swal("送信完了しました", "元の画面に戻ります", "success"); 38 39 // clear form 40 $('input, textarea').val(""); 41 $("input:checkbox").attr("checked",false); 42 $("input:radio").attr("checked",false); 43 $("option").attr("selected",false); 44 45 // reload page 46 if (page !== 'report') { 47 $('.content').empty(); 48 $('.content').load(page + '.html'); 49 getData(urlget, page); 50 } 51 52 }) 53 .fail(function(response){ 54 swal("送信失敗しました", "お手数ですが不具合報告よりご連絡ください", "error"); 55 }) 56 .always(function(response){ 57 if (page === 'report') { 58 $.ajax({ 59 type : 'POST', 60 url : "http://localhost/api/" + page + "/mail", 61 data : JSON.stringify(data), 62 contentType: 'application/json', 63 dataType : 'JSON', 64 scriptCharset : 'utf-8', 65 timeout: 10000, 66 beforeSend: function(xhr) { 67 xhr.setRequestHeader(header, token); 68 } 69 }) 70 }; 71 }); 72 }); 73}
POST後も taccountid の値が正常に入っていてGETできるのがゴールです。
拙いコードで見苦しいのですが、お知恵をお借りできればと思います。よろしくお願いいたします。
###補足情報(言語/FW/ツール等のバージョンなど)
JQuery 2.1.4
Java 1.8
SpringBoot 1.2.1
SweetAlert
MySQL
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/11/08 01:28 編集