【问题标题】:Using JQuery ajax to access a POST webservice使用 JQuery ajax 访问 POST 网络服务
【发布时间】: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


【解决方案1】:

在 pdataJSON 前面放置一个 var 来初始化它应该会让你前进。

var pdataJSON=JSON.stringify(postData);

【讨论】:

  • 是的,没错。添加了 Var,但仍然出现相同的错误
【解决方案2】:

您的数据似乎有问题:

var postData ={id:'cube1', endpoint:'myendpoint',schema:'a schema',instance:'some   
 instance'};

替换为:

  var postData ={'id':'cube1', 'endpoint':'myendpoint','schema':'a 
  schema','instance':'some instance'};

试试这个

   $.ajax({
    type: 'POST',
    contentType: 'application/json',
    url: rootURL,
    dataType: "json",
    data: JSON.stringify(postData);
    success: function(data){
        alert(data.responseText);

    },
    error: function(jqXHR, textStatus, errorThrown){
        alert('error: ' + textStatus);
    }
});

【讨论】:

  • 谢谢@user1042031。我已经听从了你的建议。我仍然收到一个错误(现在消息是错误:错误,不是很多信息吗?)但现在请求出现在 Firebug Net 透视图中。响应标头似乎没问题,但响应选项卡为空
猜你喜欢
  • 2012-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多