1

JQuery

function save() {

        imageData = $(".sigPad").signaturePad().getSignatureImage();
        consumeData = $('#consume').val();
        $.ajax({
                type: "POST",
                url: "",
                data: {'signatureasimage' : imageData, 'consume' : consumeData  },
                dataType: 'json',
                cache: false,
                success: function(response){
                    alert(response.msg);
                    /*var imageUrl = response['signature_image'];
                    d = new Date();
                    $(".signatureImage").attr("src",imageUrl);


                    if (response.status == true) {
                        window.location.href = "<?php echo ROOT_URL.'esignup/attendees_list.php?icode='.$icode;?>";
                    }*/
                },
                error: function(x,e){
                    if(x.status==0){
                        alert('You are offline!!\n Please Check Your Network.');
                    }else if(x.status==404){
                        alert('Requested URL not found.');
                    }else if(x.status==500){
                        alert('Internel Server Error.');
                    }else if(e=='parsererror'){
                        alert('Error.\nParsing JSON Request failed.');
                    }else if(e=='timeout'){
                        alert('Request Time out.');
                    }else {
                        alert('Unknow Error.\n'+x.responseText);
                    }
                }
            }); 

       };

PHP

$data = array();
    $confirmationData = array();
    $data['attendee_id'] = $attendeeId;
    $data['is_consume_the_provided_meal'] = $_POST['consume'];
    $data['signature_image'] = $destination;
    $data['confirmed'] = 1;
    if($confirmedAttendee){
        $sql = "SELECT * FROM  `".TBL_ATTENDEE_CONFIRMATION."`  WHERE `attendee_id` = '.$attendeeId.'";
        $confirmationData = selectFrom($sql);
        update_array('tbl_attendee_confirmation', $data, array('attendee_id' => $attendeeId));
        $confirmationData = selectFrom($sql);
    }else{
        var_dump("it went through insert array");
        insert_array('tbl_attendee_confirmation', $data);
    }

     $data = array();

    $data['msg']="Testing, testing.";

    echo json_encode($data);

Jquery ajax does post request with data imageData and consumeData. imageData and consumeData are strings. Copying to file works and the data updates the table. The problem is I get parsererror when I want to get imageUrl so I can update the sigImage with the new image source. I commented the part where I replace the image src with new imageURL. Does anyone know the issue?

Error shows up as "alert('Error.\nParsing JSON Request failed.');" from code. Error still shows up with test code.

diboy2
  • 11
  • 2
  • Can you please provide the full error message that you receive? –  Jul 14 '14 at 23:52
  • Just for clarification: does the success function as it stands, i.e. with the alert command and the rest commented out, work, and does it stop working when you uncomment the lines? And is your original php code like "echo json_encode($data)" (instead of the $return). – skarist Jul 15 '14 at 00:01
  • @skarist It still does not work with the current code I've provided. And yes the it $return was a different variable before. I'll change the variable and check if it still doesn't work. – diboy2 Jul 15 '14 at 00:05
  • Changed variable $return to $data and it still doesn't work. – diboy2 Jul 15 '14 at 00:08
  • ok I would open up the browser console and see what actually is comming back from the server, when the browser executes the call. You can do this be clicking the network tab in the browser console. – skarist Jul 15 '14 at 00:08
  • 1
    also check the error_log on your server, if you have access to it. Just wondering if you have php with the json lib included (json_encode). It is no in by default prior to php 5.2.x (if I remember correctly) – skarist Jul 15 '14 at 00:13
  • Thank you. I found my problem. @skarist – diboy2 Jul 15 '14 at 00:41

2 Answers2

0

Try doing this in your PHP:

echo json_encode($data, JSON_FORCE_OBJECT);

I don't completely understand it, but in my experience if you are returning an array you've built in PHP to be parsed using the ECMAScript JSON object, you need to use the JSON_FORCE_OBJECT constant to ensure that it returns a JSON object instead of a JSON array.

json_encode constants

deimal
  • 1
0

You also could try outputting the header for JSON before echoing your JSON encoded array, gimme a sec.

header('Content-Type: application/json');

Also here

Community
  • 1
  • 1
nbsp
  • 2,291
  • 1
  • 15
  • 12