How to get more information about error 500 - internal server error?

前端 未结 7 2242
粉色の甜心
粉色の甜心 2021-01-05 23:00

I use $ajax requests in many parts of my PHP website everything was working perfectly until few days ago all my $ajax requests start giving e

7条回答
  •  南笙
    南笙 (楼主)
    2021-01-05 23:33

    First of all you must be sure that the error log mechanism it's well configured.

    To do that add the following code or similar:

    ini_set("log_errors", 1);
    ini_set("error_log", "/tmp/php-error.log");
    error_log( "Php Errors!" );
    

    An other issue that I notice it's that your php script it's not returning 100% valid JSON and I also think that you can refactor the code to return the result without the necessity to create any variable (better performance, faster, lees code) avoiding any kind of memory allocations problems.

    In some cases very loaded shared servers or unscalable VPS can experiment very low memory and can randomly generate errors when try to allocate memory for variables.

    I don't know if you use some kind of Framework or not but a different way to write your script in pure php can be something like this:

    header('Content-Type: application/json');
    return (date('H') == 0 AND date('i') == 0 AND (date('s') > 0 AND date('s') < 10)) ? json_encode(['res' => 1]) : json_encode(['res' => 2]);
    

    This code will always return valid JSON format like {"res":1} or {"res":2} and you can easily access this trough JS like this res = data.res; console.log(res); and you'll evaluate this like this: (data.res === 1).

    In your case, you are using dataType: "json" and this in almost all cases it's not woking well if your php it's not returning valid JSON. Maybe you consider to remove it, it's not very important here if you are not enferced to expect only valid JSON.

    You can give it a try and see what is happening. If the error persist you can see it inside the /tmp/php-error.log file. and comment it with your hosting company.

    As a parentheses, I would like to add that your script should return 1 or 0, true or false instead of 1 or 2. It's just a more pragmatic way to doit.

    Let me know if my answer was helpful or if you can't fix the problem and I will try to help you.

    Bye Bye! Suerte!

提交回复
热议问题