The JSON data returned by the uploaded file will be prompted to download the problem solution _javascript tips

Source: Internet
Author: User

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."

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.