Daniel Campos
Daniel Campos

Reputation: 75

Ajax request returns POST <<url>> 500 (Internal Server Error)

I've been doing this module for a website using WAMP Server and everything works, but when I upladed it to the website server it always returns Error 500.

Error 500 1

Error 500 2

When the main page index.php loads (using Javascript .ready function) it send an ajax request to 'obtenermunicipio.php'.

<script type="text/javascript">
    $( document ).ready(function() {
        $.ajax({
          type: 'POST',
          url: '../obtenermunicipio.php',
          data : {'valor':'paz'},
          dataType: 'json',
          success: function(data) {
            if(data.length <= 0) {
              return;
            }
            for(var i=0; i < data.length; i++) {
                var opcion = document.createElement('option');
                opcion.innerHTML+=data[i];
                document.getElementById('unidadmedica').appendChild(opcion);
            }
          },
          error: function(e) {
            console.log(e.message);
          }
        }); 
    });
</script>

The page 'obtenermunicipio.php' receive the value 'valor' and made an MySQL query to obtain an array data that it's return.

<?php
    include($_SERVER['DOCUMENT_ROOT'].'/Noticias/conexion.php');
    header('Content-Type: text/html; charset=ISO-8859-15');
    header("application/json");
    $conexion=conectar();
    $municipio = utf8_decode($_POST['valor']);
    if($municipio != "loret")
        $sql = "SELECT DISTINCT unidadmedica FROM `unidadesmedicas` WHERE municipio like '%".$municipio."%' AND localidad NOT LIKE '%loret%'";
    else
        $sql = "SELECT DISTINCT unidadmedica FROM `unidadesmedicas` WHERE localidad like '%".$municipio."%'";
    $rs = mysql_query($sql) or die(mysql_error($conexion)); 
    $fila = mysql_fetch_all($rs);
    print json_encode($fila);
?>

any ideas of how to solve it? it works fine on Wamp.

Upvotes: 1

Views: 2171

Answers (1)

Hissvard
Hissvard

Reputation: 465

I'm putting this inside a reply instead of a comment because I feel like they were getting hard to keep track of since there are multiple things to say.

Error 500 means that there's something wrong with your PHP script that could not be handled correctly by the web server. It's usually something like an undeclared (or deprecated) function or some issue with paths or extensions.

The first step to follow in order to investigate the issue is either finding the error_log file on your web server, or pasting these two lines at the top of your php script:

<?php
error_reporting(-1); // highest error reporting level
ini_set("display_errors", true); // print errors directly as output
/* all the rest here */

That said, there is one more issue with your code! Be aware that it's really vulnerable against SQL Injection attacks since you're building the query string directly and someone else might have your query do something totally different.

To solve that, you should either use PDO and MySQLi extensions' prepared statements or pass all the variables you're going to use in your query string through mysql_real_escape_string().

Upvotes: 1

Related Questions