javascript - 带有 XML 负载的 Jquery POST 调用

标签 javascript jquery xml post

我必须要有一个服务,该服务需要以下输入消息才能成功调用。我使用 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

最佳答案

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

curl 请求使用以下内容类型:

"Content-Type: text/xml"

在标题中你可以这样设置:

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

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

data: { inputxml: escape(xmlString)}

希望这对您有所帮助!

关于javascript - 带有 XML 负载的 Jquery POST 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13060708/

相关文章:

c# - 将当前页面保存为图像

android - 像 html 一样显示 android TextViews 会 float div

java - JAVA中XML文件比较忽略节点顺序

ios - iOS 中的 XML 解析问题

javascript - 使用 jquery 和 regex 的电话号码格式

javascript - 无法在提交时验证表单

javascript - Windows 8/Metro UI 数据绑定(bind) javascript

javascript - 在传单中加载 map 非常慢

javascript - 关于Rails/JQuery/Prototype/RJS的情况

javascript - jQuery $.get 失败