1

I'm trying to get back values from php interacting with AJAX Post. I've read that I should use JSON dataType but this is the first time for me doing it and I get "SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 72 of the JSON data".

My AJAX is the following:

function apfaddpost() {
        var fd = new FormData($('#msform')[0]);
        fd.append( "main_image", $('#main_image')[0].files[0]);
        fd.append( "action", 'apf_addpost');
        $('#form-container').hide();
        $('#processing').show();
        var postProject = $.ajax({
            type: 'POST',
            url: apfajax.ajaxurl,
            data: fd,
            dataType: 'json', 
            processData: false,
            contentType: false,
        });

        postProject.done(function(data, textStatus, XMLHttpRequest) {
                $('#processing').hide();
                $('#confirm').show();
                //elements where I should display success message and link
                var success = '#success';
                var projectlink = '#projectlink';
                jQuery(success).html('');
                jQuery(success).append(data.success);
                $("#projectlink").attr("href", data.projectlink);
        });

        postProject.fail(function(MLHttpRequest, textStatus, errorThrown) {
                alert(errorThrown);
        });
    }

My php

if ( $pid != 0 )
    {   
        $message = 'Your post has been successfully added!';
        $project_link = get_permalink($pid);
        $result = array('success'=>$message,'projectlink'=>$projectlink);
        echo json_encode($result);
    }
    else {
        $message = 'Error occurred while adding the post';
        $result = array('fail'=>$message);
        echo json_encode($result);
    }

My HTML where should be printed those values is:

<div id="confirm" class="row" style="display:none">
        <div class="col-sm-12">
            <h2 class="text-center"><?php _e("Thank you!","KleeiaDev") ?></h2>
            <div class="text-center">
                <p id="success"></p><!-- Here should go the success message -->
                <p>
                    <a id="projectlink" href="">Link</a><!-- Here should go the link I'm getting as result -->
                </p>
            </div>
        </div>
    </div>

Where am I wrong?

1
  • what is the response of the ajax? Check the response. Commented Apr 23, 2016 at 13:42

1 Answer 1

1

If your PHP is outputing something else after echo json_encode($result); it will lead to that error.

Make sure you have nothing else being output. If there is no more application logic after the json_encode use exit.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help! Solved! Cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.