そのため、JQueryUI を使用して JSP で単純な Ajax 関数を使用しようとしています。フォームで使用される 2 つのテキスト フィールドを渡し、それらを 2 つの div に入力しようとしています。ボタンをクリックしても何も起こりません。console.logs を ajax 関数に入れてみましたが、何も出力されません。
ボタンクリック呼び出し用の JSP コード (要求に応じて完全な jquery)
<script type="text/javascript">
$('#saveBtn').click(function() {
//define the handler function for click event inline (it doesn't need a name because we're just using it one time)
// get the value of the search box
console.log("you are in the ajax");
var addressValue = $("#address").val();
var creditValue = $('#creditcard').val();
System.out.println("you are in the function");
// send to the server (this is relative to our current page)
$.ajax({
url: "actions.InfoAjax.action",
data: { // this is an embedded JSON object we'll send to server
address: addressValue,
creditcard: creditValue
},
dataType: 'json', // tell JQuery to expect JSON back from the server
success: function(ret) { // this is called as soon as the server returns
console.log(ret.address);
console.log(ret.creditcard);
$('#savedAddress').html(ret.address);
$('#savedCC').html(ret.creditcard);
}//success
});//ajax
});//click
});//ready
</script>
これが私のhtmlです:
<form action="post">
<input type = "text" name="address" placeholder="Address" ></input></br>
<input type = "text" name="creditcard" placeholder="Credit Card Number"> </input></br>
<button type ="button" class ="btn btn-small" id="saveBtn">Save</button>
</form>
私はこれを何時間も機能させようとしてきましたが、助けていただければ幸いです。
コンソールから取得しているエラーは次のとおりです。
readyState: 4
responseText: "
↵
↵{
↵ "address": [null],
↵ "creditcard": [ test ]
↵}
↵
↵"
1: "parsererror"
2: SyntaxError
get stack: function () { [native code] }
message: "Unexpected token e"
set stack: function () { [native code] }
__proto__: Error
callee: function () {
length: 3
__proto__: Object