developerforce / Force.com-JavaScript-REST-Toolkit

ForceTK - a minimal Force.com REST API for JavaScript apps
BSD 3-Clause "New" or "Revised" License
315 stars 175 forks source link

Create Blob method of ForceTK does not work in mozilla browser #65

Closed msrivastav13 closed 9 years ago

msrivastav13 commented 9 years ago

http://salesforce.stackexchange.com/questions/77272/file-uploader-using-forcetk-using-multipart-message-not-working-in-mozilla/77281

Please the above question .In mozilla firefox the error states that "Syntax Error unexpected End of JSON" while same code succeeds in chrome .

Chrome also the console says "Unexpected end of input"

metadaddy commented 9 years ago

I've tracked the issue down to line 326 of forcetk.js:

return this.asyncAjax ? JSON.parse(request.response) : null;

Here, the blob function returns either null, if this is an asynchronous call (the usual case), or JSON.parse(request.response) for the synchronous case.

Now the problem is that, as written above, the logic is reversed! In the usual, async case, we are trying to parse an empty response, where we should be returning null.

Easy fix - I'll push it in just a few minutes.

msrivastav13 commented 9 years ago

Thanks Pat that would be awesome :) :100: .I will be writing a blog post on how to use this with HTML 5 file API and build a simple drag and drop file uploader .Just looked at community and this feature of multi message part should make life simple .

msrivastav13 commented 9 years ago

Hello Pat.The same fix needs to be applied to like 309 as well

metadaddy commented 9 years ago

Line 309 looks ok to me - it's saying "If there is a response, parse it before handing it to the callback, otherwise pass null to the callback. Works fine in testing too.

Are you still seeing problems?

msrivastav13 commented 9 years ago

Yes my browser should me issue .same as earlier issue .The moment I changed it in line 309 it worked :).Thanks

metadaddy commented 9 years ago

Hmm - what's your line 309 now? Can you see the response in the callback?

msrivastav13 commented 9 years ago

callback(request.response ? JSON.parse(request.response) :null );

I have changed this to

callback(request.response ? null: JSON.parse(request.response) );

metadaddy commented 9 years ago

I don't understand how this can work. You're saying 'If there's something in request.response, then pass null, otherwise pass request.response', so the callback will never receive any data.

What do you see if you put console.log(request.response); just before that line?

msrivastav13 commented 9 years ago

A blank string "" .I will check once more and try to put screenshot once I am free .Also my code is little modified and reads from HTML file API as a parameter .Thank you

sahilbatraa commented 9 years ago

I am still facing issue in the same , now the line is different and it says "SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data" for line " callback(request.response ? JSON.parse(request.response) : null);" in below code snippet.

if (this.asyncAjax) { request.onreadystatechange = function() { // continue if the process is completed if (request.readyState == 4) { // continue only if HTTP status is good if (request.status >= 200 && request.status < 300) { // retrieve the response callback(request.response ? JSON.parse(request.response) : null); } else if(request.status == 401 && !retry) { that.refreshAccessToken(function(oauthResponse) { that.setSessionToken(oauthResponse.access_token, null,oauthResponse.instance_url); that.blob(path, fields, fileName, file, callback, error, true, progress); }, error); } else { // return status message error(request, request.statusText, request.response); } }
} }

Do we need to make the same change in this line too ???

msrivastav13 commented 9 years ago

Sahil,

I did the same thing to that line and it worked as well .Was explaining same to Pat in all this thread :).Thanks for confirmation.Pat looks like we need same change in line 309 in the file .

sahilbatraa commented 9 years ago

Did u get the response after changing this line ???

ghost commented 9 years ago

I'm trying to understand why the JSON.parse fails. What do you see logged if you put

console.log(request.response);

on the line before the callback?

sahilbatraa commented 9 years ago

Mohit,

This causes response as null if we change the line. I wil try that and will let you know.

sahilbatraa commented 9 years ago

Pat ,

we have a response as if we see in post request :+1: : <?xml version="1.0" encoding="UTF-8"?>068K0000000AXXXXXXtrue</Result

and error as :SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

callback(request.response ? JSON.parse(request.response) : null);

ghost commented 9 years ago

Ah - you're seeing XML! This makes more sense now! Not sure why you're getting an XML response, but I can force it to be JSON.

I'm on the train right now - will be able to send you a patch in the next couple of hours.

On Thursday, May 28, 2015, sahilbatraa notifications@github.com wrote:

Pat ,

we have a response as if we see in post request [image: :+1:] : <?xml version="1.0" encoding="UTF-8"?>068K0000000AqXXXXXtrue</Result

and error as :SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

callback(request.response ? JSON.parse(request.response) : null);

— Reply to this email directly or view it on GitHub https://github.com/developerforce/Force.com-JavaScript-REST-Toolkit/issues/65#issuecomment-106444997 .

Cheers,

Pat

sahilbatraa commented 9 years ago

Yes , In chrome we are getting JSON as output and same in mozilla is giving as XML output. Sure force it to JSON . Please send me the patch . Many thanks in advance. mozilla chrome

metadaddy commented 9 years ago

@sahilbatraa - in forcetk.js, just before line 296 (setting the authorization header), insert the following line:

request.setRequestHeader('Accept', 'application/json');

That should ensure that the API responds with JSON. Let me know if it works for you, and I'll push the fix to Github. Please leave line 309 as

callback(request.response ? JSON.parse(request.response) : null);

Otherwise, you'll never get a response from the API, so you won't see the record ID, etc.

sahilbatraa commented 9 years ago

@metadaddy , It worked !! :100: :+1:

Thanks a lot :) you can push the fix.

mozilla_fix

metadaddy commented 9 years ago

Great - will do!

@msrivastav13 - you should set the accept header, rather than flipping the logic in the callback.

msrivastav13 commented 9 years ago

@metadaddy-sfdc Awesome .Thanks will do .Was missing something on this and Now realize the issue .Thanks so much guys @sahilbatraa