I have this jQuery code:
<script type="text/javascript">
$(document).ready(function()
{
$('.vote_up').click(function()
{
alert ( "test: " + $(this).attr("data-problem_id") );
problem_id = $(this).attr("data-problem_id");
var dataString = 'problem_id='+ problem_id + '&vote=+';
$.ajax({
type: "POST",
url: "/problems/vote.php",
dataType: "json",
data: dataString,
success: function(json)
{
// ? :)
alert (json);
}
error : function()
{
alert("ajax error");
}
});
//Return false to prevent page navigation
return false;
});
$('.vote_down').click(function()
{
alert("down");
problem_id = $(this).attr("data-problem_id");
var dataString = 'problem_id='+ problem_id + '&vote=-';
//Return false to prevent page navigation
return false;
});
});
</script>
I also have this link HTML:
<p class="half_text"><?php echo $upvotes; ?> <strong><a class="vote_up" style="color: #295B7B; font-weight:bold;" href="#" data-problem_id="<?php echo $problem_id; ?>">Vote Up</a></strong> | <?php echo $downvotes; ?> <strong><a class="vote_down" style="color: #295B7B; font-weight:bold;" href="#" data-problem_id="<?php echo $problem_id; ?>">Vote Down</a></strong></p>
When I press the link, I get the Javascript console error "Syntax error: unexpected identifier"
You can reproduce this by clicking on a vote-up link here
Any idea why this is happening and how to fix it?
I have this jQuery code:
<script type="text/javascript">
$(document).ready(function()
{
$('.vote_up').click(function()
{
alert ( "test: " + $(this).attr("data-problem_id") );
problem_id = $(this).attr("data-problem_id");
var dataString = 'problem_id='+ problem_id + '&vote=+';
$.ajax({
type: "POST",
url: "/problems/vote.php",
dataType: "json",
data: dataString,
success: function(json)
{
// ? :)
alert (json);
}
error : function()
{
alert("ajax error");
}
});
//Return false to prevent page navigation
return false;
});
$('.vote_down').click(function()
{
alert("down");
problem_id = $(this).attr("data-problem_id");
var dataString = 'problem_id='+ problem_id + '&vote=-';
//Return false to prevent page navigation
return false;
});
});
</script>
I also have this link HTML:
<p class="half_text"><?php echo $upvotes; ?> <strong><a class="vote_up" style="color: #295B7B; font-weight:bold;" href="#" data-problem_id="<?php echo $problem_id; ?>">Vote Up</a></strong> | <?php echo $downvotes; ?> <strong><a class="vote_down" style="color: #295B7B; font-weight:bold;" href="#" data-problem_id="<?php echo $problem_id; ?>">Vote Down</a></strong></p>
When I press the link, I get the Javascript console error "Syntax error: unexpected identifier"
You can reproduce this by clicking on a vote-up link here http://www.problemio.com
Any idea why this is happening and how to fix it?
Share Improve this question asked Oct 5, 2011 at 20:37 GenadinikGenadinik 18.6k64 gold badges191 silver badges288 bronze badges2 Answers
Reset to default 19no comma between your success
and error
callbacks.
In my case I was using the wrong syntax on the server side.
Make sure you use:
return json_encode($result)
instead of
echo json_encode($result)