jQuery ajax转换器。可能吗?

问题描述 投票:0回答:1

我可以为我自己的mime类型创建convertor:

    $.ajax( url, {
        accepts: { dload: 'application/x-dload' },
        contents: { dload: /dload/ },
        converters: {
            "text dload": jQuery.parseJSON,
        },
        dataType: 'dload',
        success: function( data, status, xhr ){
            ... data is of dload type
        },
    })

但是当响应不是文本时,是否有可能为我的mime类型提供转换器?例如xml或html?

这不起作用:

    $.ajax( url, {
        accepts: { dload: 'application/x-dload' },
        contents: { dload: /dload/ },
        converters: {
            "text dload": jQuery.parseJSON,
            "xml dload": convert_xml_to_dload,
            "html dload": convert_html_to_dload,
        },
        dataType: 'dload',
        success: function( data, status, xhr ){
            ... data is of dload type
        },
    })
jquery ajax
1个回答
0
投票

我没有测试它,但查看jQuery文档http://api.jquery.com/jquery.ajax/#using-converters你可能需要这样的东西:

converters: {
    "text dload": true,
    "dload json": jQuery.parseJSON,
    "dload xml": convert_xml_to_dload, // or jQuery.parseXML
    "dload html": convert_html_to_dload,
}
© www.soinside.com 2019 - 2024. All rights reserved.