javascript - jquery unexpected token on radio click -
here's code
// when dom ready $(document).ready(function() { // 'click' because ie likes choke on 'change' $('input[name=passedparameter]').click(function(e) { // prevent normal, boring, tedious form submission e.preventdefault(); // send server out-of-band xhr $.post('http://localhost/topostthepost.php', function() { data: $(this).val(), success: function(resp) { if(resp == '1') { alert('saved successfully'); } else { alert('oops, went wrong!'); } } }); }); });
im new jquery, in javascript,
far thought, logically possible , right code,
-when open response page, (http://localhost/topostthepost.php
), went good,
-im using php framework, need post "http://localhost/
" got right url,
-when run page, wont thing, inspect element (chrome) it, , warns unexpected token
what missed? jquery/javascript deny http://
in $.post
?
ps:code credit karim79
it's subtle:
// when dom ready $(document).ready(function() { // 'click' because ie likes choke on 'change' $('input[name=passedparameter]').click(function(e) { // prevent normal, boring, tedious form submission e.preventdefault(); // send server out-of-band xhr $.post('http://localhost/topostthepost.php', function() { // problem here ========================= ^^^^^^^^^^ <<========== data: $(this).val(), success: function(resp) { if(resp == '1') { alert('saved successfully'); } else { alert('oops, went wrong!'); } } }); }); });
it shouldn't have function
there, you're passing object. function
, :
after success
becomes syntax error. (the 1 after data
fine because looks label, comma @ end of line means we're still within expression, :
after success
culprit.)
but note that's not how call $.post
, it's similar how call $.ajax
. here's updated code without syntax error , switching $.ajax
:
// when dom ready $(document).ready(function() { // 'click' because ie likes choke on 'change' $('input[name=passedparameter]').click(function(e) { // prevent normal, boring, tedious form submission e.preventdefault(); // send server out-of-band xhr $.ajax({ url: 'http://localhost/topostthepost.php', type: 'post', data: $(this).val(), success: function(resp) { if(resp == '1') { alert('saved successfully'); } else { alert('oops, went wrong!'); } } }); }); });
however, bit still looks dodgy:
data: $(this).val(),
that means you're passing string call. if give string, must already encoded. must either encode it, or pass object instead jquery can encode you:
data: {someparametername: $(this).val()},
...where someparametername
server in form data server-side. that's passing object data
parameter call. object has (in case) 1 property, i've called someparametername
above, value $(this).val()
.
when send post
system, default it's application/x-www-form-urlencoded
data, fundamentally consists of names , values — instance, form field names , values of fields. in above, i'm sending field name someparametername
, value $(this).val()
. because we're passing data
object, jquery handle encoding names , values us.
on server receiving post
, in post data, using field name someparametername
) value.
obviously you'd use more appropriate parameter name someparametername
.
here's hypothetical example giving more 1 field:
data: { firstname: $("input[name=firstname]").val(), lastname: $("input[name=lastname]").val() }
there i'm posting 2 fields (parameters), names firstname
, lastname
.
Comments
Post a Comment