0

The JSON returned by my PHP is:

{"success":0,"message":"Error: No entityId passed!"}

Still my javascript tells me "SyntaxError: Unexpected end of input".

PHP:

    ...

    //check if an image id was passed for removal in the POST data
    if ( isset($_POST["entityId"])) {
        $entityId = $_POST["entityId"];
    }
    else {
        //setup response json
        $resp = array();
        $resp['success'] = 0;
        $resp['message'] = "Error: No entityId passed!";

        header('Content-Type: application/json');
        echo json_encode($resp);
    }

    ...

JS:

             // send xhr request
             $.ajax({
                 dataType: 'json',
                 type: $theForm.attr('method'),
                 url: $theForm.attr('action'),
                 data: $theForm.serialize(),
                 success: function(data) {
                     //backend returns a json with variable success either true or false.
                     resp = data;
                     if(resp.success == 1) {
                        //render gallery anew
                     }
                     else {
                        console.log(data);
                        if (data.message) {
                            $(self).find('.VanillaGallery-overlayContentWrapper').html(data.message);       
                        }
                        else {
                            $(self).find('.VanillaGallery-overlayContentWrapper').html('Oops! Något gick fel, felmeddelande saknas dock.');       
                        }
                     }

                 },
                 error: function(xhr, status, text) {
                     $(self).find('.VanillaGallery-overlayContentWrapper').html('Oops! Något gick fel...<br />'+text);
                 }
             });

Very strange. My headers are different depending on if the request is made through ajax or as a plain separate request directly in browser (not through ajax):

The headers look like this through ajax:

Connection:Keep-Alive
Content-Length:0
Content-Type:text/html
Date:Wed, 01 Apr 2015 17:48:26 GMT
Keep-Alive:timeout=5, max=97
Server:Apache/2.2.25 (Unix) mod_ssl/2.2.25 OpenSSL/0.9.8zc DAV/2 PHP/5.5.3
X-Pad:avoid browser bug
X-Powered-By:PHP/5.5.3

And through this directly from browser adress bar:

Connection:Keep-Alive
Content-Length:52
Content-Type:application/json
Date:Wed, 01 Apr 2015 17:42:23 GMT
Keep-Alive:timeout=5, max=100
Server:Apache/2.2.25 (Unix) mod_ssl/2.2.25 OpenSSL/0.9.8zc DAV/2 PHP/5.5.3
X-Powered-By:PHP/5.5.3
6
  • 2
    Have you checked the developer tools to verify that the HTTP response really looks like what you think it looks like? Commented Apr 1, 2015 at 15:28
  • Could it be that my localhost (Apache on MAC, MAMP) is not configured to give a proper JSON response Commented Apr 1, 2015 at 17:28
  • My network log in Chrome says 'application/json' does that mean that the server (localhost) is giving a proper json response? Commented Apr 1, 2015 at 17:43
  • I'm pretty sure that jQuery won't care what the content type header says. If you tell it it's JSON it'll attempt to parse it. What the network tab in the developer tools will let you see is whether your server is really sending back the response you think it is. You'll see the actual response content, in other words. Commented Apr 1, 2015 at 17:57
  • I added the responses. How come it is interpreted as application/json in one instance, and text/html when passed over ajax? Commented Apr 1, 2015 at 18:54

2 Answers 2

1

Ok, so it is obvious once you look at it with fresh eyes... in the ajax request an entityId IS passed (not obvious from the sample code since form data isn't displayed...) and thus the first IF condition in the PHP code above evaluates to true. And in that clause, there is no output, no echo of any sort. That's why I get "unexpected end of input".

And as for testing it by running directly in browser adress bar, that way the PHP will of course land in the else-bracket of my PHP code above, and actually give a response since there is no POST-data at all doing it that way...

Sorry for taking up your time, sometimes one is just too tired...

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

Comments

0

SyntaxError: Unexpected end of input it's commonly related to some mistake/typo in JS (or even PHP), btw your code, standalone, is working fine. You should check the rest... maybe you missed a bracket or a semicolon somewhere.

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.