上傳文件返回的json數(shù)據(jù)會(huì)被提示下載問(wèn)題解決方案
最近項(xiàng)目中出現(xiàn)上傳文件返回的json數(shù)據(jù)會(huì)被提示下載,只有在ie10+中才會(huì)出現(xiàn)這個(gè)問(wèn)題。前端使用jQuery的插件ajaxForm提交表單,后臺(tái)返回的數(shù)據(jù)格式為json。代碼如下:
后端Python:
def jsonp(func):
"""Wraps JSONified output for JSONP requests."""
@wraps(func)
def decorated_function(*args, **kwargs):
callback = request.args.get('callback', False)
temp_content = func(*args, **kwargs)
if isinstance(temp_content, dict):
temp_content.setdefault('success', True)
temp_content.setdefault('code', 200)
try:
temp_content = json.dumps(temp_content, indent=4)
except UnicodeDecodeError:
try:
temp_content = ujson.dumps(temp_content)
except StandardError as e:
logger.exception(e)
temp_content = json.dumps({'success': False, 'code': 500, 'info': 'INVALID_CONTENT'})
temp_content = cgi.escape(temp_content)
if callback:
# 依據(jù) http://evilcos.me/?p=425,jsonp添加/**/頭部會(huì)安全一些
content = '/**/' + str(callback) + '(' + temp_content + ')'
mimetype = 'application/javascript'
headers = {'charset':'utf-8'}
return current_app.response_class(content, mimetype=mimetype,headers=headers)
else:
mimetype = 'application/json'
headers = {'charset':'utf-8'}
content = temp_content
return current_app.response_class(content, mimetype=mimetype,headers=headers)
elif isinstance(temp_content, basestring):
temp_content = cgi.escape(temp_content)
return temp_content
else:
return temp_content
return decorated_function
@mod.route('/patch/install.json', methods=['POST'])
@jsonp
def patch_install():
return {'data': 'data'}
前端js代碼:
$('#form').ajaxSubmit({
url : '/patch/install.json',
type : 'post',
dataType : 'json',
iframe : true,
success: function(res) {
// code
}
});
解決辦法:
需要將后端返回的數(shù)據(jù)格式改成text/html格式的,如下:
def plain(func):
"""wrap text/html reponse"""
@wraps(func)
def _inner(*args, **kwargs):
resp = func(*args, **kwargs)
if isinstance(resp, dict):
resp.setdefault('success', True)
resp.setdefault('code', 200)
resp = json.dumps(resp)
resp = cgi.escape(resp)
return current_app.response_class(resp, mimetype='text/html', headers={'charset': 'utf-8'})
elif isinstance(resp, basestring):
resp = cgi.escape(resp)
return current_app.response_class(resp, mimetype='text/html', headers={'charset': 'utf-8'})
else:
return resp
return _inner
@mod.route('/patch/install.json', methods=['POST'])
@plain
def patch_install():
return {'data': 'data'}
注意:此例后端是用Python,如果項(xiàng)目中遇到同樣問(wèn)題,改成對(duì)應(yīng)語(yǔ)言
總結(jié),其實(shí)解決這個(gè)問(wèn)題,簡(jiǎn)單的說(shuō)就一句話“將后端返回的數(shù)據(jù)格式改成text/html格式的”
相關(guān)文章
微信小程序全局配置之tabBar實(shí)戰(zhàn)案例
tabBar相對(duì)而言用的還是比較多的,但是用起來(lái)并沒(méi)有難,下面這篇文章主要給大家介紹了關(guān)于微信小程序全局配置之tabBar的相關(guān)資料,文中通過(guò)圖文以及示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
JavaScript通過(guò)改變文字透明度實(shí)現(xiàn)的文字閃爍效果實(shí)例
這篇文章主要介紹了JavaScript通過(guò)改變文字透明度實(shí)現(xiàn)的文字閃爍效果,結(jié)合完整實(shí)例形式分析了javascript基于定時(shí)器周期性動(dòng)態(tài)修改頁(yè)面元素屬性的相關(guān)操作技巧,需要的朋友可以參考下2017-04-04
JavaScript函數(shù)增強(qiáng)以及額外知識(shí)
函數(shù)就是封裝了一段可以被重復(fù)執(zhí)行調(diào)用的代碼塊,下面這篇文章主要給大家介紹了關(guān)于JavaScript函數(shù)增強(qiáng)以及額外知識(shí)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06
jQuery實(shí)現(xiàn)仿百度首頁(yè)滑動(dòng)伸縮展開(kāi)的添加服務(wù)效果代碼
這篇文章主要介紹了jQuery實(shí)現(xiàn)仿百度首頁(yè)滑動(dòng)伸縮展開(kāi)的添加服務(wù)效果代碼,通過(guò)jQuery相應(yīng)鼠標(biāo)事件控制頁(yè)面元素的動(dòng)態(tài)變換功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-09-09
微信小程序點(diǎn)擊item使之滾動(dòng)到屏幕中間位置
這篇文章主要介紹了微信小程序點(diǎn)擊item使之滾動(dòng)到屏幕中間位置,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
JavaScript正則表達(dá)式小結(jié)(test|match|search|replace|split|exec)
這篇文章主要介紹了JavaScript正則表達(dá)式小結(jié)(test|match|search|replace|split|exec)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2016-12-12
用document.documentElement取代document.body的原因分析
ll建議用document.documentElement代替document.body2009-11-11
javascript實(shí)現(xiàn)行拖動(dòng)的方法
這篇文章主要介紹了javascript實(shí)現(xiàn)行拖動(dòng)的方法,涉及javascript鼠標(biāo)事件及頁(yè)面元素的相關(guān)操作技巧,需要的朋友可以參考下2015-05-05
基于javascript滾動(dòng)圖片具體實(shí)現(xiàn)
這篇文章主要介紹了javascript滾動(dòng)圖片具體實(shí)現(xiàn),有需要的朋友可以參考一下2013-11-11

