jQuery ajax 转换器.可能吗



我可以为我自己的 mime 类型创建转换器:

    $.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文档 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,
}

最新更新