gpt4 book ai didi

web-services - 使用 JQuery ajax 访问 POST Web 服务

转载 作者:行者123 更新时间:2023-12-01 06:01:04 25 4
gpt4 key购买 nike

我已经实现了 Jersey Web 服务,我想使用 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>

如有任何帮助,我们将不胜感激!

问候,

洛雷娜

最佳答案

将 var 放在 pdataJSON 前面来初始化它应该会让您前进。

var pdataJSON=JSON.stringify(postData);

关于web-services - 使用 JQuery ajax 访问 POST Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11450555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com