Home > Web Front-end > JS Tutorial > The json data returned by uploading the file will be prompted to download the problem solution_javascript skills

The json data returned by uploading the file will be prompted to download the problem solution_javascript skills

WBOY
Release: 2016-05-16 16:29:28
Original
1440 people have browsed it

In recent projects, the json data returned by uploading files will be prompted to download. This problem only occurs in IE10. The front-end uses the jQuery plug-in ajaxForm to submit the form, and the data format returned by the background is json. The code is as follows:

Backend Python:

Copy code 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 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:
                    # Based on http://evilcos.me/?p=425, jsonp adds /**/The head 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:

Copy code The code is as follows:

$('#form').ajaxSubmit({
URL : '/patch/install.json',
Type : 'post',
DataType: 'json',
iframe : true,
Success: function(res) {
             // code
}
});

Solution:
It is necessary to change the data format returned by the backend to text/html format, as follows:

Copy code The code is as follows:

def plain(func):
"""wrap text/html response"""
@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 of this example uses Python. If you encounter the same problem in the project, change it to the corresponding language

To summarize, in fact, to solve this problem, simply put it in one sentence: "Change the data format returned by the backend to text/html format"

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template