简体   繁体   English

使用JQuery ajax访问POST Web服务

[英]Using JQuery ajax to access a POST webservice

I have implemented a Jersey web service and I want to access it using JQuery. 我已经实现了Jersey Web服务,并且想使用JQuery访问它。 It seems that ajax() method is what I need. 似乎我需要ajax()方法。

I've followed the suggestions found here but I keep getting an error and I don't know what to do next. 我遵循了在这里找到的建议但是我一直遇到错误,并且不知道下一步该怎么做。 The error that appears in the alert is [object Object] 警报中显示的错误是[object Object]

I've already tested my web service using a Java client and the following curl command, and in both cases it is returning what I expect (actually the service just modifies one of the properties of the object and returns it, cause I'm just testing communication issues) 我已经使用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}

Here is my html page: 这是我的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>

Any help would be appreciated! 任何帮助,将不胜感激!

regards, 问候,

Lorena 洛雷娜(Lorena)

将var放在pdataJSON前面以对其进行初始化应该会使您前进。

var pdataJSON=JSON.stringify(postData);

Looks like you have problem with your data: 看起来您的数据有问题:

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

replace with : 用。。。来代替 :

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

Try this 尝试这个

   $.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);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM