最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - getJSON: NetworkError: 500 Internal Server Error - Stack Overflow

programmeradmin2浏览0评论

I wrote a code that uses getJSON (see below). FireBug returns the following error message:

"NetworkError: 500 Internal Server Error - http://localhost:10088/test/myquery.php?query=SELECT%20tm%20FROM%20schedule%20WHERE%20val=%27BT009%27;"

I cannot figure out the reason of this error. Any help is highly appreciated.

PS I tested this SQL query in MySQL Query Browser. It has returned 3 rows.

mainPage.php

<script>
function updateList(){
   var query = "SELECT tm FROM schedule WHERE val='BT009';";

   $.getJSON(
           'myquery.php?query='+query,
           function(data) 
           {
               alert(data);
           }   
   );

}

$(document).ready(function() {
    updateList();
});

</script>

myquery.php

<?php
include_once 'include/connect_db.php';

if (isset($_GET['query'])) {
    $query = $_GET['query'];

    $condb = connectDB();
    $result=execute_query($query);
    closeDB($condb);

    $rows = array();

    if ($result && mysql_num_rows($result)) {
        while($row = mysql_fetch_array($result)) {
            $rows[] = $row['tm'];
        }
    }   

    var_dump($rows);

    echo json_encode($rows);

} else {
    echo json_encode("Failed");
}
?>

I wrote a code that uses getJSON (see below). FireBug returns the following error message:

"NetworkError: 500 Internal Server Error - http://localhost:10088/test/myquery.php?query=SELECT%20tm%20FROM%20schedule%20WHERE%20val=%27BT009%27;"

I cannot figure out the reason of this error. Any help is highly appreciated.

PS I tested this SQL query in MySQL Query Browser. It has returned 3 rows.

mainPage.php

<script>
function updateList(){
   var query = "SELECT tm FROM schedule WHERE val='BT009';";

   $.getJSON(
           'myquery.php?query='+query,
           function(data) 
           {
               alert(data);
           }   
   );

}

$(document).ready(function() {
    updateList();
});

</script>

myquery.php

<?php
include_once 'include/connect_db.php';

if (isset($_GET['query'])) {
    $query = $_GET['query'];

    $condb = connectDB();
    $result=execute_query($query);
    closeDB($condb);

    $rows = array();

    if ($result && mysql_num_rows($result)) {
        while($row = mysql_fetch_array($result)) {
            $rows[] = $row['tm'];
        }
    }   

    var_dump($rows);

    echo json_encode($rows);

} else {
    echo json_encode("Failed");
}
?>
Share Improve this question asked Aug 11, 2012 at 10:55 GusgusGusgus 3532 gold badges6 silver badges22 bronze badges 8
  • 1 Please don't use the mysql_* functions, they are no longer maintained and munity has begun the deprecation process . Instead you should learn about prepared statements and use either PDO or MySQLi. If you cannot decide, this article will help to choose. If you want to learn, here is a good PDO-related tutorial. – vascowhite Commented Aug 11, 2012 at 10:58
  • 2 This is not the answer to your question, but it'd be irresponsible of me not to mention it. You are generating an SQL string in JavaScript and sending it to your server for execution. That's a very serious application security flaw called SQL Injection. Don't do this. Really, really, really don't. Here's information about it on the OWASP site. – Barend Commented Aug 11, 2012 at 10:59
  • 2 What happens if I use the URL yourdomain./myquery.php?query="drop table schedule"? – vascowhite Commented Aug 11, 2012 at 11:00
  • @vascowhite or worse, dropping the entire DB. – Joseph Commented Aug 11, 2012 at 11:01
  • @JosephtheDreamer unfortunately I didn't know the db name otherwise I'd have done that :) – vascowhite Commented Aug 11, 2012 at 11:03
 |  Show 3 more ments

3 Answers 3

Reset to default 3

An Internal Server Error (Code 500) is a server-side error and nothing to do with AJAX. This is usually caused by bad server-side code. You might want to check your syntax and also try viewing the page via the browser. The page may have printed some error details as well.

I'm going to venture a guess that the server is having difficulty understanding your query parameter. Try encoding it:

'myquery.php?query='+encodeURIComponent(query)

Like someone else stated, what you are doing is not secure. You should not be passing queries for a service to execute!

EDIT: Another possibility is that you aren't passing three arguments to $.getJSON(). Try:

$.getJSON(
   'myquery.php',
   {query: query},
   function(data) {
      alert(data);
   }   
);

Try using firebug.Run the firebug when you are executing this code in the browser.It will give the cause of the error and also where the correction is required.Hope it will help.

发布评论

评论列表(0)

  1. 暂无评论