Closed msrivastav13 closed 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.
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 .
Hello Pat.The same fix needs to be applied to like 309 as well
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?
Yes my browser should me issue .same as earlier issue .The moment I changed it in line 309 it worked :).Thanks
Hmm - what's your line 309 now? Can you see the response in the callback?
callback(request.response ? JSON.parse(request.response) :null );
I have changed this to
callback(request.response ? null: JSON.parse(request.response) );
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?
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
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 ???
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 .
Did u get the response after changing this line ???
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?
Mohit,
This causes response as null if we change the line. I wil try that and will let you know.
Pat ,
we have a response as if we see in post request :+1:
:
<?xml version="1.0" encoding="UTF-8"?>
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);
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
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.
@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.
@metadaddy , It worked !! :100: :+1:
Thanks a lot :) you can push the fix.
Great - will do!
@msrivastav13 - you should set the accept header, rather than flipping the logic in the callback.
@metadaddy-sfdc Awesome .Thanks will do .Was missing something on this and Now realize the issue .Thanks so much guys @sahilbatraa
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"