简体   繁体   中英

Using JQuery ajax to access a POST webservice

I have implemented a Jersey web service and I want to access it using JQuery. It seems that ajax() method is what I need.

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]

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)

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:

<!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

将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);
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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