- 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 = = "json") {
Eval ("data = " + 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