所以我试图在使用 JQueryUI 的 JSP 中使用一个简单的 Ajax 函数。我只是传入表单中使用的两个文本字段,并尝试用它们填充两个 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