Ajaxfileupload the JSON data returned by the server cannot be resolved after the file has been uploaded successfully

Source: Internet
Author: User

    • Ajaxfileupload is a very useful file upload plugin, there are many versions of it on the Internet, but when uploading a file to return JSON data to the foreground, there will be a situation where JSON data cannot be parsed.

Careful debugging found that after submitting data to the server, it entered the success callback function, but did not parse the JSON data. That means that the server responds, into the success method, the only problem is that the foreground accepts the data is not in JSON format.

Using Console.log output Data discovery is not purely JSON data, with more head <pre style= "Word-wrap:break-word; White-space:pre-wrap; " >{"Jsonkey": "Jsonvalue"}</pre>

Find the problem is very good to deal with, although set the Data-type parameter is JSON type, but still appear this bug, and then look at the source of the enlightened

===== here is the content of the source code ==========

if (type = = &quot;json&quot;) {
Eval (&quot;data = &quot; + data);
}
=======================================
The JSON data that was originally returned is generated by eval, meaning that the accepted data is always of the text type and is only converted according to Data-type parameters, because ajaxfileupload is uploading files asynchronously by creating an iframe layer.

According to this principle, there are 2 ways to solve this problem.
1: At the front desk will <pre style= "Word-wrap:break-word; White-space:pre-wrap; " >{"Jsonkey": "Jsonvalue"}</pre> filter, this is clean JSON data
var reg =/<pre.+?> (. +) <\/pre>/g;  var result = Data.match (reg);  data = regexp.$1;
2: Filter The parameters of eval in the source code directly
if (type = = "json") {
data = Jquery.parsejson (jQuery (data). text ());
}
or let the server return the text type of data and convert it to JSON.

Ajaxfileupload The JSON data returned by the server cannot be resolved after the file is uploaded successfully

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.