【问题标题】:JQuery Ajax POST XML structure / Filter ChainJQuery Ajax POST XML 结构/过滤器链
【发布时间】:2011-09-08 08:20:09
【问题描述】:

我想通过 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 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>

谢谢

【问题讨论】:

  • 并且您正在接收 xml 响应?
  • 你试过答案了吗?你有什么错误吗?
  • 是的,仍然出现 http 500 错误,内部服务器错误,我将添加 xml 尝试发布.. 等待编辑。谢谢(现在:))
  • 你将不得不转义引号等看到这个答案stackoverflow.com/q/3099369/413670,如果它被发布,也尝试发送一个简单的xml,比如"&lt;test&gt;&lt;/test&gt;" se
  • 我的结果仍然是空的,但是 ajax 帖子正在运行并运行到成功函数中......这是一个很好的步骤。谢谢,还有什么要注意的吗?像 mime-type 什么的?

标签: javascript xml jquery


【解决方案1】:
$.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);
    } 
}); 

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

jQuery ajax post to web service

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 只需创建一个对象,将其作为数据发送,例如 { 'test' : 'test' }
    • 如果将数据设置为xml,jquery会自动将对象转换为xml吗?
    猜你喜欢
    • 1970-01-01
    • 2018-01-08
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    相关资源
    最近更新 更多