The JSON data returned by the upload file in the most recent project is prompted to download, and this problem only occurs in ie10+. The front end uses the jquery plug-in ajaxform to submit the form, and the data returned in the background is formatted as JSON. The code is as follows:
Back-end Python:
Copy Code code 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 Unicodedecodeerror:
Try
Temp_content = Ujson.dumps (temp_content)
Except StandardError as E:
Logger.exception (e)
Temp_content = Json.dumps ({' Success ': False, ' code ': +, ' info ': ' invalid_content '})
Temp_content = Cgi.escape (temp_content)
If callback:
# Adding/**/to the head based on HTTP://EVILCOS.ME/?P=425,JSONP is 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:
Copy Code code as follows:
$ (' #form '). Ajaxsubmit ({
URL: '/patch/install.json ',
Type: ' Post ',
DataType: ' JSON ',
Iframe:true,
Success:function (res) {
Code
}
});
Solution:
The data format returned by the backend needs to be changed to text/html format, as follows:
Copy Code code 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: The backend for this example is Python, and if the project encounters the same problem, change to the corresponding language
Summing up, in fact, to solve this problem, simply say "The data format returned by the backend to text/html format."