jquery - 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,
}
推荐阅读
- python - 从像计数器这样的列表列表中计算每个组合的最快方法是什么?
- angular - 不能绑定到“ngModel”,因为它不是“输入”的已知属性?
- java - 如何用 rxjava 替换使用 volatile 变量的线程间通信?
- python - 在损失函数中索引 tf 变量
- dependency-injection - 升级后使用 Autofac 在 NServiceBus 中注册依赖关系
- caching - Android Webview 的本地文件路径
- python - 形状计算器循环
- ios - 将 UIImageView 调整为 UILabel 的高度
- elasticsearch - 如何扩展弹性搜索日期范围直方图聚合查询?
- python - Python:在构建器外部实例化类时引发警告