2012-10-25 36 views
2

我不得不希望以下輸入消息成功調用的服務。我使用curl調用服務。與XML負載的Jquery POST調用

POST /airavata-registry-rest-services/registry/api/hostdescriptor/save HTTP/1.1 
User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4 libidn/1.23 librtmp/2.3 
Host: 127.0.0.1 
Accept: text/plain 
Content-Type: text/xml 
Content-Length: 191 

<type:hostDescription xmlns:type="http://schemas.airavata.apache.org/gfac/type"> 
    <type:hostName>LocalHost11</type:hostName> 
    <type:hostAddress>127.0.0.1</type:hostAddress></type:hostDescription> 

我使用的curl命令是;

curl -H "Accept: text/plain" -X POST -H "Content-Type: text/xml" -d '<type:hostDescription xmlns:type="http://schemas.airavata.apache.org/gfac/type"><type:hostName>LocalHost11</type:hostName><type:hostAddress>127.0.0.1</type:hostAddress></type:hostDescription>' http://localhost:6060/airavata-registry-rest-services/registry/api/hostdescriptor/save 

我試圖通過Jquery調用相同的服務,我無法生成相同的請求。我寫的代碼是;

var hostName = $("#hostName1").val(); 
    var hostAddress = $("#hostAddress1").val(); 
    var xml = $('<type:hostDescription xmlns:type="http://schemas.airavata.apache.org/gfac/type"><type:hostName>' + hostName + '</type:hostName><type:hostAddress>' + hostAddress + '</type:hostAddress></type:hostDescription>'); 

    var xmlData= $(xml); 
    var xmlString; 
    if (window.ActiveXObject){ 
     xmlString = xmlData.xml; 
    } else { 
     var oSerializer = new XMLSerializer(); 
     xmlString = oSerializer.serializeToString(xmlData[0]); 
    } 
    console.log(xmlString); 

    $.ajax({ 
     headers: { 
      Accept : "text/plain; charset=utf-8", 
      "Content-Type": "text/plain; charset=utf-8", 
      "Accept-Encoding" : "" 
     }, 
     type: "POST", 
     url: "http://localhost:6060/airavata-registry-rest-services/registry/api/hostdescriptor/save", 
     data: xmlString, 
     dataType: "xml", 
     cache: false, 
     error: function() { alert("No data found."); }, 
     success: function(xml) { 
      alert("it works"); 
     } 
    }).done(function(msg) { 
       alert("Data Saved: " + msg); 
      }); 

上述方法生成的請求如下所示。它也缺少消息正文。你能建議我如何改變我的jQuery功能來調用上述服務。

OPTIONS /airavata-registry-rest-services/registry/api/hostdescriptor/save HTTP/1.1 
Host: 127.0.0.1 
Connection: keep-alive 
Access-Control-Request-Method: POST 
Origin: http://localhost:7080 
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.11 (KHTML, like Gecko) Ubuntu/12.04 Chromium/20.0.1132.47 Chrome/20.0.1132.47 Safari/536.11 
Access-Control-Request-Headers: origin, contenttype, content-type, accept 
Accept: */* 
Referer: http://localhost:7080/client-api-demo/x_host_descriptor_save.html 
Accept-Encoding: gzip,deflate,sdch 
Accept-Language: en-US,en;q=0.8 
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3 
+0

您是否錯過了數據中的參數名稱,請問您的curl請求是怎樣的? – felipeclopes

+0

@felipeclopes我編輯我的問題有curl命令。 – user915745

回答

0

我只注意到您的AJAX請求中curl請求中傳遞的Content-Type中的差異。

的捲曲請求使用下面的Content-Type:

"Content-Type: text/xml" 

在標題你可能會這樣設置的:

headers: { 
      Accept : "text/plain; charset=utf-8", 
      "Content-Type": "text/xml; charset=utf-8" 
     }, 

而且我相信經過時,你是不是包括參數名稱數據。您的數據可能與參數來設置它屬於,這取決於你在你的服務期待名稱:

data: { inputxml: escape(xmlString)} 

希望這有助於!

+0

它工作嗎? – felipeclopes