国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

上傳文件返回的json數(shù)據(jù)會被提示下載問題解決方案

2024-05-06 16:11:19
字體:
供稿:網(wǎng)友
這篇文章主要介紹了項目中python+js實現(xiàn)的上傳文件返回的json數(shù)據(jù)會被提示下載問題解決方案,需要的朋友可以參考下
 
 

最近項目中出現(xiàn)上傳文件返回的json數(shù)據(jù)會被提示下載,只有在ie10+中才會出現(xiàn)這個問題。前端使用jQuery的插件ajaxForm提交表單,后臺返回的數(shù)據(jù)格式為json。代碼如下:

 后端Python:

 

復(fù)制代碼代碼如下:

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添加/**/頭部會安全一些
                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代碼:

 

復(fù)制代碼代碼如下:

$('#form').ajaxSubmit({
    url      : '/patch/install.json',
    type     : 'post',
    dataType : 'json',
    iframe   : true,
    success: function(res) {
        // code
    }
});

 

解決辦法:
  需要將后端返回的數(shù)據(jù)格式改成text/html格式的,如下:

 

復(fù)制代碼代碼如下:

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,如果項目中遇到同樣問題,改成對應(yīng)語言

總結(jié),其實解決這個問題,簡單的說就一句話“將后端返回的數(shù)據(jù)格式改成text/html格式的”


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 嘉黎县| 郧西县| 仙游县| 光山县| 璧山县| 江源县| 肃北| 白水县| 木兰县| 香格里拉县| 惠州市| 巴彦淖尔市| 汝南县| 博乐市| 玉田县| 玛沁县| 衡东县| 政和县| 甘孜县| 汝阳县| 霍州市| 东莞市| 游戏| 泰宁县| 新乡县| 佛冈县| 双桥区| 喀什市| 那坡县| 咸阳市| 河西区| 和政县| 孝感市| 安岳县| 三门峡市| 晋中市| 澄迈县| 高平市| 枝江市| 凤凰县| 林西县|