Chat Bubble

JavaScript / jQuery

  • RSS
Viewing 1 to 5 (5 Total)
SyntaxError: JSON.parse: unexpected character parsererror

zizaheer

Total Posts: 3
Joined: Dec 2011

Can't figure out where I made the mistake!! I checked it with jsonlint, its showing parse error on line 1 but I think problem is somewhere else. My data gets saved in DB and in firebug I can see the response from server which is 'error':true msg:'oops!' 200OK , but this error is not being shown in my error , instead it was showing that parsererror mentioned in title. I am new to js, Please help. JS file given below:

JS


$.ajax({

type: 'POST',
url: 'http://localhost/Insert.php',
data: formData,
dataType: 'json',
cache: false,
timeout: 7000,
success: function(data){
$('form #response').removeClass().addClass((data.error === true) ? 'error' : 'success')
.html(data.msg).fadeIn('fast');

if($('form #response').hasClass('success')){
setTimeout("$('form #response').fadeOut('fast')", 5000);
}
},
error: function(XMLHttpRequest, textStatus, errorThrown){

$('form #response').removeClass().addClass('error')
.html('There was ' + errorThrown +
' error due to ' + textStatus +
' condition ').fadeIn('fast');
},
complete: function(XMLHttpRequest, status){
$('form')[0].reset();
}

});


PHP (just to test js)

$tmp = false;
if ($tmp == false) {
$return['error'] = true;
$return['msg'] = 'oops!';
echo json_encode($return);
exit();
} else {
$return['error'] = false;
$return['msg'] = 'Okidoki!';
echo json_encode($return);
exit();
}

Tags:
Posted on December 20, 2011 at 10:39 AM
Display Messages: Threaded     Flat
4 Replies

Admin

Admin
Total Posts: 65
Joined: Apr 2011

re: SyntaxError: JSON.parse: unexpected character parsererror

What version of the jQuery library are you using?

Posted on December 20, 2011 at 4:26 PM

zizaheer

Total Posts: 3
Joined: Dec 2011

re: SyntaxError: JSON.parse: unexpected character parsererror

I'm using JQuery Version 1.7.1.

Posted on December 22, 2011 at 10:56 AM

zizaheer

Total Posts: 3
Joined: Dec 2011

re: SyntaxError: JSON.parse: unexpected character parsererror

Hi Aaron,
I found out where I made the mistake. I should have mentioned my JS file correctly here. its the target url param in ajax function. i thought its not required. lol. Anyway, this was my real coding which was giving me the parse error:

............
var URL='http://localhost/user/'
$.ajax({
type: 'POST',
url: URL + 'Insert.php',
............
............

When I mentioned url: 'http://localhost/user/Insert.php' it worked fine.

Posted on January 07, 2012 at 7:35 AM

Admin

Admin
Total Posts: 65
Joined: Apr 2011

re: SyntaxError: JSON.parse: unexpected character parsererror

Glad you found the error, sorry I wasn't any help.smiling

Posted on January 07, 2012 at 9:36 AM