2

I've a simple contact form, and using Ajax to process it with a PHP file...

The problem is: it works just fine on the localhost, but it does NOT on the live server!, it gives me on the console:

[object Object]
parsererror
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

This is my code:

PHP:

if (isset($error) && !empty($error)) {
    exit(json_encode(["error" => $error]));
}

And

exit(json_encode(["success" => "success message"]));

jQuery Ajax:

$.ajax({
    url: "./includes/form-processor.php",
    method: "POST",
    data: data,
    dataType: 'json',
    success: function (data, textStatus, jqXHR) {
        console.log(data);
        console.log(data["error"]);
        console.log(data.error);
        if ('undefined' == data.error || data.error == null) {
            message_window.text(data["success"]);
        } else {
            message_window.text(data["error"]);
        }
    },
    error: function (jqXHR, textStatus, errorThrown) {
        console.log(jqXHR + "\n" + textStatus + "\n" + errorThrown);
    }
});

I've read a lot of similar questions here on stack overflow but most of the answers for them mentioned that it happens because they're parsing the data JSON by jQuery and return it from the back-end as JSON again!

So, actually that make me not sure if I'm returning it from the PHP file right or not!

I've tried to return it from the PHP this way:

exit(["success" => "success message"]);

And

exit("success" => "success message");

And

exit("success message");

But it doesn't work as well!

Also, as I said earlier

it works just fine on the localhost, but it does NOT on the live server!

So, that sounds weird! o.O

What should I check to make it works on the live server?

Thanks

Homer
  • 355
  • 1
  • 5
  • 17
  • What does `console.log(data);` actually give you in your console? Do you have error reporting on? – Rasclatt May 24 '16 at 23:39
  • 4
    `["success" => "success message"]` is a new array syntax introduced in PHP 5.4 so your production server is probably under 5.4. You should use `array("success"=>"success message")` instead – Cave Johnson May 24 '16 at 23:39
  • 1
    @Andrew you beat me to it, that is what I was going to suggest. – Rasclatt May 24 '16 at 23:40
  • 1
    Do what @Andrew said, and make sure your PHP is sending it **as** json by directing the header before you return - `header("Content-Type: application/json");` – Darren May 24 '16 at 23:43
  • @Rasclatt , the `console.log(data)` on the **localhost** returns the `error message` for the form correctly, it's just a simple hint asking to fill the blank input. – Homer May 24 '16 at 23:44
  • Sorry I meant on the live, that is where the error is happening. What does it say there? – Rasclatt May 24 '16 at 23:45
  • @Rasclatt , it gives the error on the question at the very top: `[object Object] parsererror SyntaxError....` – Homer May 24 '16 at 23:47
  • Sorry, just comment out the `dataType` and return the raw data so do `// dataType: 'json',` then see what `data` says. Also make sure you have `ini_set('display_errors',1); error_reporting(E_ALL);` at the top of your php page so you can see what error(s), if any, are returned. – Rasclatt May 24 '16 at 23:51
  • @Rasclatt , sure, after commenting the `dataType` it still returns the same error: `[object Object] parsererror SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data` ... and just to make you certain, this is how I'm returning from PHP now: `exit(json_encode(["success" => "success message"]));` – Homer May 24 '16 at 23:58
  • 1
    That is strange, shouldn't have returned an object but rather a json string. Did you already try using the word-version of array `exit(json_encode(array("success" => "success message")));`? – Rasclatt May 25 '16 at 00:01
  • @Rasclatt , sorry, I wasn't! .... Yay! it works! ^_^ – Homer May 25 '16 at 00:12
  • Thanks a lot @Andrew and you guys as well! I appreciate your help! :) – Homer May 25 '16 at 00:14
  • 1
    @Andrew , if you wanted to copy your comment as an answer and I choose it that would be cool! Thanks again. – Homer May 25 '16 at 00:16
  • 1
    @Homer sure thing. – Cave Johnson May 25 '16 at 00:17

1 Answers1

2

["success" => "success message"] is a new array syntax introduced in PHP 5.4 so your production server is probably under 5.4. You should use array("success"=>"success message") instead. For your specific code, use:

exit(json_encode(array("success" => "success message")));

and

exit(json_encode(array("error" => $error)));
Cave Johnson
  • 6,499
  • 5
  • 38
  • 57