【发布时间】:2012-07-12 11:10:30
【问题描述】:
我已经实现了 Jersey 网络服务,我想使用 JQuery 访问它。 看来 ajax() 方法是我需要的。
我已按照 here 的建议进行操作,但我不断收到错误消息,我不知道下一步该做什么。警报中出现的错误是[object Object]
我已经使用 Java 客户端和以下 curl 命令测试了我的 Web 服务,在这两种情况下它都返回了我所期望的(实际上该服务只是修改了对象的一个属性并返回它,因为我'我只是在测试沟通问题)
lorena@lorena-virtual-machine:~/tools$ echo $DATA
--data-binary {"endpoint":"endpoint","instance":null,"id":"idcube","schema":null}
lorena@lorena-virtual-machine:~/tools$ echo $HEADER
--header Content-Type:application/json
lorena@lorena-virtual-machine:~/tools$ echo $URL
http://localhost:8888/rest/cubes/get
lorena@lorena-virtual-machine:~/tools$ curl ${DATA} ${HEADER} ${URL}
这是我的html页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>QBplus: OLAP cubes in RDF</title>
<link rel="stylesheet" type="text/css" href="css/style.css" />
<script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="js/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
var baseURL = "http://localhost:8888/rest/cubes/get";
var postData ={id:'cube1', endpoint:'myendpoint',schema:'a schema',instance:'some instance'};
var pdataJSON=JSON.stringify(postData);
$.ajax({
type:'POST',
url: baseURL,
data:pdataJSON,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (responseText) {
alert(responseText.d);
},
error: function (error) {
alert(error);
}
});
});//ready
</script>
</head>
<body>
<h1>CUBES</h1>
<div id="cubes"></div>
</body>
任何帮助将不胜感激!
问候,
洛丽娜
【问题讨论】:
-
显然 jQuery stringify 在 IE7 等浏览器中会出现问题。也许你根本不需要使用它。
-
pdataJSON未初始化。 -
"但我不断收到错误,我不知道下一步该做什么"您遇到什么错误?发布一些细节!
-
感谢@Stefan,反正我在 Ubuntu 下使用的是 Firefox
-
这是我的问题之一@Anders。使用错误:function(jqXHR, textStatus, errorThrown){ alert('error: ' + textStatus);我只是得到错误:错误
标签: web-services jquery jersey