1

All, I've got the following code:

onDone: function (e, data) {
var result = jQuery.parseJSON(data.result);
var uploadType = result.upload_type;
var filename = result.name;
var insert_id = result.insert_id;
}

When I run this I get the following error from IE:

Message: 'upload_type' is null or not an object

When I do a console.log(data.result) in Chrome I get the following data:

 [{
    "name": "1_3266_671641333369_14800358_42187036_5237378_n.jpg",
    "size": 35535,
    "type": "image\/jpeg",
    "url": "\/web\/upload\/1_3266_671641333369_14800358_42187036_5237378_n.jpg",
    "thumbnail_url": "\/web\/upload\/thumbnails\/1_3266_671641333369_14800358_42187036_5237378_n.jpg",
    "delete_url": "\/web\/upload.php?file=1_3266_671641333369_14800358_42187036_5237378_n.jpg",
    "delete_type": "DELETE",
    "upload_type": "video_montage",
    "insert_id": "288"
}]

I'm trying to see why I'm getting the error message in IE8 but I can't see what is being parsed. When I did do an alert like this:

var result = jQuery.parseJSON(data.result);
alert(result)

I was getting null but no idea why.

Any help is greatly appreciated on how to read it or why my results of the parsed JSON are null even though there is data in my returned string!

Thanks!

user1048676
  • 9,756
  • 26
  • 83
  • 120

4 Answers4

2

I finally found the solution to this and it's an issue because IE doesn't support XHR uploads. You can find a detail explanation of this by checking out this post:

jquery file upload - IE done callback data.result issue

That didn't quite solve my needs however because that code only worked for IE and I needed to work across browsers. I updated my code a little bit and if you check out the accepted answer here:

jquery file upload - IE callback results in object Object

You'll find out how to handle a JSON response from IE for an XHR type of upload!

Community
  • 1
  • 1
user1048676
  • 9,756
  • 26
  • 83
  • 120
1

data.result is already an object.

And you're getting null because jQuery returns null when the parameter of jQuery.parseJSON() isn't of type string

Andreas
  • 21,535
  • 7
  • 47
  • 56
  • 1
    @user1048676: Remove the `parseJSON` line. Also, try `data.result[0].upload_type`, your data is an array (of objects). – gen_Eric Jul 11 '12 at 22:51
  • As @Rocket suggests. Remove the line with `parseJSON` so jQuery won't return `null`. After this you should be able to get the data with `data.result[0].upload_type` – Andreas Jul 11 '12 at 22:54
0

Maybe try this in IE:

alert(data.result);

That should give you the string value of the JSON.

cdmckay
  • 31,832
  • 25
  • 83
  • 114
0

data.result is already parsed, no need for $.parseJSON. Remove this line:

var result = jQuery.parseJSON(data.result);

Also, your JSON object is an array (of objects). Try getting the property from the 1st object in the array.

var uploadType = data.result[0].upload_type;
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • I get undefined when I do that. – user1048676 Jul 11 '12 at 22:53
  • What does `console.log(data.result[0])` say? – gen_Eric Jul 11 '12 at 22:55
  • I think IE just prints the incredibly useful `[Object object]` when you log an object. – cdmckay Jul 11 '12 at 22:58
  • It just says [ and then it gives me an error and says the result is undefined. – user1048676 Jul 11 '12 at 22:59
  • @cdmckay: That's why I hate IE's dev tools. I use [Firebug Lite](http://getfirebug.com/firebuglite/) :-P – gen_Eric Jul 11 '12 at 22:59
  • @user1048676: It prints a `'['`? That means it might *not* be parsed. Do me a favor. `console.log(typeof data.result)`. – gen_Eric Jul 11 '12 at 23:00
  • @user1048676: Didn't you say that `alert(data.result);` alert said `[object Object]`? How is that possible? – gen_Eric Jul 11 '12 at 23:02
  • @Rocket In chrome it gives me the string, in IE8 it gives me [object Object] – user1048676 Jul 11 '12 at 23:04
  • @user1048676 Include `json2.js` from https://github.com/douglascrockford/JSON-js/ and do `console.log(JSON.stringify(data))` and tell us what it says. – cdmckay Jul 11 '12 at 23:06
  • @cdmckay I did this in in Chrome and got the following message: Uncaught TypeError: Converting circular structure to JSON – user1048676 Jul 11 '12 at 23:08
  • @user1048676: Try that in IE. – gen_Eric Jul 11 '12 at 23:08
  • @Rocket I get console is not defined in IE – user1048676 Jul 11 '12 at 23:09
  • @user1048676: Open the dev tools, then load your page. IE is stupid, and `console` only exists if the dev tools are opened *first*. Or try `alert(JSON.stringify(data))` instead. – gen_Eric Jul 11 '12 at 23:10
  • @Rocket I changed it to console.log(JSON.stringify(data.result)) and got the following: LOG: {"length":1,"prevObject":{"length":1},"selector":".contents()"} – user1048676 Jul 11 '12 at 23:13
  • @user1048676: That's not the same object you show in your question. – gen_Eric Jul 11 '12 at 23:14
  • @Rocket When I do console.log(data.result) in Chrome that is the object that I get back in my console. – user1048676 Jul 11 '12 at 23:16
  • @Rocket And if I run console.log(data.result) in IE I get the object Object – user1048676 Jul 11 '12 at 23:17
  • @user1048676: So `console.log`ing the same object gives different results in different browsers? Just out of curiosity, where is this object coming from? What is `onDone` a callback for? – gen_Eric Jul 11 '12 at 23:18
  • @Rocket I'm using the following theme: http://revaxarts-themes.com/?t=whitelabel. The onDone is basically a callback when an image is uploaded using their Form -> Multi File Upload. I'm basically returning some data from an upload.php script that is handling the upload. – user1048676 Jul 11 '12 at 23:20
  • @user1048676: Why would `upload.php` return different values in IE and Chrome? – gen_Eric Jul 11 '12 at 23:21
  • @Rocket I have no idea. I can add the script to my question so you can see it. There is nothing out of the ordinary that I'm doing so I'm not sure. – user1048676 Jul 11 '12 at 23:23
  • @Rocket The PHP upload file is actually this one: https://github.com/blueimp/jQuery-File-Upload/blob/master/server/php/upload.class.php. The line that basically returns the file info is line 347. – user1048676 Jul 11 '12 at 23:39
  • @Rocket Could this happen because all of my files aren't in the same directory (but all in the same domain)? It looks like there is an issue already documented for this: https://github.com/blueimp/jQuery-File-Upload/issues/659 – user1048676 Jul 12 '12 at 00:14
  • @cdmckay Do you think it could happen because of my files aren't in the same directory (but all in the same domain)? – user1048676 Jul 12 '12 at 00:15
  • @user1048676: I don't know ='( – gen_Eric Jul 12 '12 at 00:59
  • 1
    @Rocket Just in case you were curious, I narrowed the issue down even further. Check out this post on here: http://stackoverflow.com/questions/8814068/jquery-file-upload-ie-done-callback-data-result-issue I still don't know how to solve my issue but should shed some light for you on why different browsers are returning different results! – user1048676 Jul 12 '12 at 02:50
  • @Rocket Just an FYI, I finally solved it. Check out my answer on here to find out what I had to do. – user1048676 Jul 12 '12 at 16:38
  • @cdmckay Just an FYI, I finally solved it. Check out my answer on here to find out what I had to do. – user1048676 Jul 12 '12 at 16:38