Javascript JQuery Ajax POST XML 结构/过滤器链

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7345107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:54:25  来源:igfitidea点击:

JQuery Ajax POST XML structure / Filter Chain

javascriptxmljquery

提问by dotchuZ

I want to Post an XML structure via AJAX to get a filtered result set. The webservice is able to handle post requests, but something seems to be wrong with my POSTing.

我想通过 AJAX 发布 XML 结构以获取过滤的结果集。网络服务能够处理发布请求,但我的发布似乎有问题。

$.ajax({
    url: ajaxurl,
    data: {
        inputxml: escape('<test></test>') <- how to post xml structure correctly?
    }, 
    type: 'POST',
    contentType: "text/xml",
    dataType: "text",
    success : parse,
    error : function (xhr, ajaxOptions, thrownError){  
        alert(xhr.status);          
        alert(thrownError);
    } 
}); 

XML:

XML:

<?xml version="1.0" encoding="UTF-8"?>
<f:filterChain
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:f="urn:foo">
    <f:filter attributeId="number">
        <f:rangeCondition conditionSign="INCLUSION" operator="BETWEEN">
            <f:low>5</f:low>
            <f:high>15</f:high>
        </f:rangeCondition>
    </f:filter>
</f:filterChain>

Thanks

谢谢

回答by Rafay

$.ajax({
    url: ajaxurl,
    data: "<test></test>", 
    type: 'POST',
    contentType: "text/xml",
    dataType: "text",
    success : parse,
    error : function (xhr, ajaxOptions, thrownError){  
        console.log(xhr.status);          
        console.log(thrownError);
    } 
}); 

see this SO answer it may help

看到这个 SO 答案它可能会有所帮助

jQuery ajax post to web service

jQuery ajax 发布到 Web 服务

回答by Bas Slagter

Maybe it's best to set your values in an object and send that object over to the server as xml by setting the dataType of the ajaxmethod to 'xml'.

也许最好在一个对象中设置您的值,然后通过将ajax方法的 dataType 设置为 'xml'将该对象作为 xml 发送到服务器。