上傳文件返回的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)文章
uni-app操作數(shù)據(jù)庫(kù)的三種方法總結(jié)
數(shù)據(jù)庫(kù)操作的,可以采用多方案,下面這篇文章主要給大家介紹了關(guān)于uni-app操作數(shù)據(jù)庫(kù)的三種方法,文中通過(guò)實(shí)例代碼和圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-05-05淺談一個(gè)webpack構(gòu)建速度優(yōu)化誤區(qū)
這篇文章主要介紹了淺談一個(gè)webpack構(gòu)建速度優(yōu)化誤區(qū),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-06-06原生javascript實(shí)現(xiàn)分享到朋友圈功能 支持ios和android
本文主要介紹網(wǎng)上一個(gè)牛人寫的js可以實(shí)現(xiàn)在UC瀏覽器和QQ瀏覽器中調(diào)用瀏覽器內(nèi)置的分享組件進(jìn)行分享。2016-05-05基于es6三點(diǎn)運(yùn)算符的使用方法(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇基于三點(diǎn)運(yùn)算符的使用方法(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10詳解JavaScript Promise和Async/Await
這篇文章主要介紹了JavaScript Promise和Async/Await,對(duì)異步編程感興趣的同學(xué),可以參考下2021-04-04