【发布时间】:2013-10-23 10:44:33
【问题描述】:
我是“jQuery ajax”的新手,我正在使用 ajax 将请求发送到服务器。我正在发送数据,即 no、name、class、age 作为正常参数。我的要求是,我想以 JSON 格式发送数据。我的代码是,
function fun1() {
$.ajaxSetup({
jsonp: null,
jsonpCallback: null
});
$.ajax({
type: 'POST',
url: 'register.action',
dataType: 'json',
data: {
no: document.getElementById("id1").value,
name: document.getElementById("id2").value,
clas: document.getElementById("id3").value,
age: document.getElementById("id4").value
},
success: function (data) {
printStudentDetails(data);
},
error: function () {
alert("failure");
}
});
}
我的服务器端资源是java。
【问题讨论】:
-
你的问题/问题是什么?
-
您可能应该使用 POST 发送 json 数据 - 我什至不确定 GET 会对您的数据做什么。例如,它可能只编码:?no=JUNK&name=JUNK。另外,添加
contentType: 'application/json' -
查看 Java JSONObject json.org/java 我假设您想将 java 对象呈现为 json
-
正如 Blazes 所说:更改为
type: 'post'