This article mainly introduces how to download the json data returned from uploaded files implemented by python + js in the project, if you want to download the json data returned from the uploaded file in a recent project, you will be prompted to download the data. This problem occurs only in ie10 +. The front-end uses the jQuery plug-in ajaxForm to submit a form. The data returned by the background is in json format. The Code is as follows:
Backend Python:
The Code is as follows:
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 t UnicodeDecodeError:
Try:
Temp_content = ujson. dumps (temp_content)
Counter t 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:
# Based on http://evilcos.me /? P = 425, jsonp Add/**/header will be safer
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 '}
Front-end js Code:
The Code is as follows:
$ ('# Form'). ajaxSubmit ({
Url: '/patch/install. json ',
Type: 'post ',
DataType: 'json ',
Iframe: true,
Success: function (res ){
// Code
}
});
Solution:
You need to change the format of the data returned by the backend to text/html, as follows:
The Code is as follows:
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 '}
Note: In this example, the backend uses Python. If the same problem occurs in the project, change it to the corresponding language.
To sum up, we can solve this problem simply by saying, "convert the format of the data returned by the backend to text/html format"