簡體   English   中英

使用JQuery ajax訪問POST Web服務

[英]Using JQuery ajax to access a POST webservice

我已經實現了Jersey Web服務,並且想使用JQuery訪問它。 似乎我需要ajax()方法。

我遵循了在這里找到的建議但是我一直遇到錯誤,並且不知道下一步該怎么做。 警報中顯示的錯誤是[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>

任何幫助,將不勝感激!

問候,

洛雷娜(Lorena)

將var放在pdataJSON前面以對其進行初始化應該會使您前進。

var pdataJSON=JSON.stringify(postData);

看起來您的數據有問題:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM