我试图通过AJAX向我的Java后端发送一个JSon格式的对象,但是没有成功。我想知道语法是否正确。
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="scripts/jquery.min.js.download"></script>
<script src="scripts/jquery.imagemapster.js.download"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
var myJSon = {"name":"Jo","age":30,"city":"Ny"};
$.ajax({
type: "POST",
url: 'http://localhost:8080/Servidor/server',
//contentType: "application/json;charset=utf-8",
dataType: "json",
data: JSON.stringify(myJSon),
success: function (data){
alert('Sucess');
},
error: function () {
alert('Error');
}
});
});
});
</script>
</head>
<body>
<button>Send an HTTP POST request to a page and get the result back</button>
</body>
</html>
当行contentType: "application/json;charset=utf-8"没有被注释掉时,我在后端得到以下错误:INFO: Could not find grammar element for class java.lang.String
语法正确吗?错误会不会来自后端本身?
附言:对不起,我的英语不好
发布于 2018-01-18 22:26:56
您的代码很好,只是在AJAX调用中拼写错了"success“(您需要2个字母c而不是1)。所以用success: function(data){...} https://jsfiddle.net/stephentillman/aow5pah2/替换sucess: function(data){...}
https://stackoverflow.com/questions/48323256
复制相似问题