2

我必须提供一项服务,该服务需要以下输入消息才能成功调用。我使用 curl 调用了该服务。

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
4

1 回答 1

0

我只是注意到在您的 curl 请求中传递的 Content-Type 与您的 AJAX 请求有所不同。

curl 请求使用以下 Content-Type:

"Content-Type: text/xml"

在标题中,您可以这样设置:

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

另外我相信您在传递数据时没有包括参数名称。您的数据可能会使用它所属的参数进行设置,具体取决于您在服务中期望的名称:

data: { inputxml: escape(xmlString)}

希望这可以帮助!

于 2012-10-25T11:25:17.573 回答