0

I'm trying to work out why this function isn't working by placing a few alerts all the way through it.

I get the first 'hmmm' alert, but then nothing after that.

If I look at firebug, I can see that I actually get the returned json data I desire, so obviously the ajax request is a success.... but so why is nothing on the inside of the success return doing anything?

alert('hmm'); // <- i work

$.ajax({
type: 'POST',
url: '/process_registration.php',
data: {event: event_id, time: time_id, ticket: ticket_id, tix: number_tickets, sta: cc_state, type: cc_type, e: reg[0], m: reg[1], f:reg[2], l:reg[3], cc:reg[4], cvn:reg[5], mo:reg[6], yr:reg[7], st:reg[8], city:reg[9], p:reg[10], tot:total },
cache: false,
dataType: 'json',
success: function(data) {

alert('hi1'); // <- why don't I get inside here and work?

        if(data['success']=='1'){
        window.location.href = '/registration_complete';
        }

    if(data['unknown']=='1' ||  data['error']=='1') {

        alert('hi your submission failed');

    }

 }

});

I'm used to seeing some errors if this sort of thing happens but I'm not getting anything back from the browser to debug it. I'm sure its something simple and will reveal a new dimension to my stupidity.

Please help :)

Here is an example submission of the data which firebug says is being posted in the ajax post:

cc  3242324232423242
city    oasuht
cvn 122
e   tohaeus@email.com
event   22
f   soatehu
l   sntahou
m   23423424
mo  12
p   234234
st  aoesntuh asotenhu
sta State
ticket  37 
time    120
tix 4
tot 4.00
yr  12

So it appears all the data is going across and shouldn't be the issue to me.

willdanceforfun
  • 11,044
  • 31
  • 82
  • 122
  • 1
    Remove `data` parameter and tell us what you see. If it changes nothing, then please go to the URL it should call and then tell us what happens (eg. is there a redirection of some sort?). – Tadeck Nov 15 '11 at 05:20
  • It changes nothing I'm afraid - I still get a return of the json data, in this case -> "{"error":"1"}{"error_msg":" Invalid Expiry Date. Your credit card has not been billed for this transaction."}" because we didn't send any data for the expiry date of the credit card. – willdanceforfun Nov 15 '11 at 05:23
  • 1
    add `error: function(xhr,status,error) {...}` callback and see if that's being called because of an error. – Strelok Nov 15 '11 at 05:33
  • A alerted those and got: xhr: [object XMLHttpRequest] status: parsererror error: SyntaxError: Unexpected token { – willdanceforfun Nov 15 '11 at 05:37
  • 2
    I think your script isn't returning valid json data.... – Sal Nov 15 '11 at 05:40
  • Possibly - does this look invalid though? {"error":"1"}{"error_msg":" Invalid Expiry Date. Your credit card has not been billed for this transaction."} – willdanceforfun Nov 15 '11 at 05:42
  • I just ran it through a validator and you are right sally!! the json is invalid. – willdanceforfun Nov 15 '11 at 05:44
  • 1
    It was just a guess mate, that's good it worked for you :) – Sal Nov 15 '11 at 05:45

3 Answers3

2

Your using the success callback, which won't be called if the request is unsuccessful.

Take a look at the other callbacks:

  • complete
  • error
  • statusCode
Blender
  • 289,723
  • 53
  • 439
  • 496
  • Thanks for that i'll check them out. But isn't it successful if I'm getting the returned json data? Doesn't 'success' simply mean it found the file and loaded? The json data I'm getting back is from this processing of the ajax loaded file... – willdanceforfun Nov 15 '11 at 05:28
  • @apaidnerd: Thanks. Not sure why I didn't include it in my exhaustive list. – Blender Nov 15 '11 at 05:28
  • I think that means that your JSON is invalid. Try running it through a validator and see where it hiccups. – Blender Nov 15 '11 at 05:57
1

did you check it with firebug to see the exact error you are getting? You can also add an error function in your ajax call. If that is hit, there is some problem with your data :)

You can use json2.js and use JSON.stringify to post your data. This might work

Community
  • 1
  • 1
Rohan
  • 1,705
  • 7
  • 17
  • 38
  • try putting an error function too and let me know if it is hit. its always good to have one – Rohan Nov 15 '11 at 05:29
  • no rocket science in that. insert a , after success and then add error: function(jqXHR, textStatus, errorThrown) {} – Rohan Nov 15 '11 at 05:36
  • Then this should solve your issue: http://stackoverflow.com/questions/631418/jquery-getjson-ajax-parseerror – Rohan Nov 15 '11 at 05:38
  • "xhr: [object XMLHttpRequest] status: parsererror error: SyntaxError: Unexpected token {" – willdanceforfun Nov 15 '11 at 05:38
  • add a breakpoint and scrutinize your data for invalid tokens. see my edit for a possible answer to your problem – Rohan Nov 15 '11 at 05:50
1

write exit; right after you json_encode($data); in your php file and then give it a try.

kaizer1v
  • 898
  • 8
  • 20