-1

I know there are many questions out there on the same topic and I've read all of them but they don't help my case.

I am trying to parse some JSON data returned from my serverside PHP script. I've used JSONLint to verify that the PHP output is a valid JSON string.

I have the following ajax code:

$(document).ready(function(){
        $('#update-stats-submit').on("click", function(){
            if (requestRunning) { // don't do anything if an AJAX request is pending
                return;
            }
            $.ajax({
                type: "GET",
                url: "calculate.php",
                data: "q="+$("#table-info").val(),
                dataType: "json",
                success: function(data){
                    $("#update-result").animate({ opacity: 100 });
                    $("#update-result").html(data.output_msg);
                    $("#update-result").delay(3000).animate({ opacity: 0 });
                    setTimeout(function() {
                        $("#update-result").empty();
                    }, 4000);
                    alert(data.avg + "\n" + data.var + "\n" + data.count + "\n" + data.est + "\n" + data.min + "\n" + data.max);
                },
                error: function(xhr, ajaxOptions, thrownError){
                    $("#update-result").html(xhr.responseText + "\n" + xhr.status + "\n" + thrownError);
                }
            })
            return false;
        });
    });

I've not gotten this piece of code to execute successfully. Each time the following error is returned.

200 SyntaxError: Unexpected end of input

Sample JSON output returned from calculate.php:

{
"output_msg":"Success!",
"avg":5.79916666667,
"var":4.63505345486,
"n":40,
"est":"1",
"min":"3",
"max":"4"
}

Any tips would be greatly appreciated.

26
  • 1
    possible duplicate of json parsing error syntax error unexpected end of input Commented Jan 17, 2015 at 5:25
  • pretty certain is not a duplicate Commented Jan 17, 2015 at 5:29
  • What is the Response supposed to be? What do you get when you type the url directly in the browser? Commented Jan 17, 2015 at 5:29
  • Which line causes the error? Commented Jan 17, 2015 at 5:31
  • "q="+$("#table-info").val(), should really be {q:$("#table-info").val()}, so it gets encoded correctly. If not, you should be using encodeURIComponent() Commented Jan 17, 2015 at 5:31

1 Answer 1

0

Basically there's nothing wrong with the above ajax script. It appears the bug lied with the serverside PHP code that allowed the script to exit() under certain GET request conditions.

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

Comments

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.