【发布时间】:2014-10-03 16:09:30
【问题描述】:
大家好。当我尝试为我的 JAX-WS 服务(我使用 google jsonwebservice 库)制作 ajax 时,我遇到了一个问题。 所以在我的页面上我有这个代码:
function submit(){
var JSONObject= '{sayHello:{name:"alexei"}}';
console.log(JSONObject);
$.ajax({
type: 'POST',
url: '/jaxwsExample-1.0.0-SNAPSHOT/json/hello',
contentType: 'application/json',
data: JSON.stringify(JSONObject),
dataType: 'application/json',
async: true,
success: function(data) {
console.log("DATA " + data);
}
});
}
还有console.log(JSONObject);回到我身边:
{sayHello:{name:"alexei"}}
但是当我在我的 TomCat 服务器中时,我收到了这个错误:
原因:com.jaxws.json.codec.JSONFault:无效的 JSON 输入:"{sayHello:{name:\"alexei\"}}"
但我也从http://code.google.com/p/jsonwebservice/wiki/GettingStarted得到了工作示例
在该示例中,使用了进行 ajax 调用的prototype.js 框架:
function submit(){
new Ajax.Request('/jaxwsExample-1.0.0-SNAPSHOT/json/hello', {
method: 'post',
contentType: 'application/json',
postBody: '{"sayHello":{"name":"'+$('name').value+'"}}',
onSuccess: function(transport) {
$('response').update(transport.responseText).setStyle({ background: '#FFFFAA' });
$('notice').update(transport.responseText.evalJSON().message).setStyle({ background: '#dfd' });
}
});
}
但我想用 jquery 代替prototype.js,你能帮我看看我的 jquery ajax 调用或 JSON 对象有什么问题吗?
【问题讨论】:
-
将 JSON 对象中的键括在引号中:
{"sayHello":{"name":"alexei"}} -
我仍然遇到同样的错误
标签: jquery ajax json jax-ws prototypejs