AJAX 在 GoDaddy 主机上出现数据类型 JSON 解析错误


AJAX with dataType JSON parse error on GoDaddy hosting

>编辑:从那以后,我把所有东西都切换到了mysqli。我有另一个脚本可以在 mysqli 中正确执行所有操作,但是在匆忙中,我复制了其他人的一部分,而没有注意到它是在 mysql 中完成的。看起来这解决了这个问题,谢谢大家!我在下面编辑了我的代码,以便它可以用作任何正在与类似问题作斗争的人的资源。

我在让 ajax 在 godaddy 共享主机帐户上工作时遇到了一些麻烦。我以前在另一个服务器上使用它没有任何麻烦,所以我认为 godaddy 服务器有些奇怪,尽管我很乐意不正确。我首先尝试用perl编写这段代码,但事实证明goaddy不支持 JSON.pm,所以我切换到php,因为这似乎是godaddy选择的语言。我仍然在 ajax 调用中抛出错误。

这是每个部分的玩具版本。HTML中唯一相关的部分是对覆盖的调用.js以及测试ID部分,以显示jQuery正在工作。

下面的 HTML 调用 overlay.js:

<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>Carl Thomas Edwards</title>
  <!--[if IE]>
    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  <![endif]-->
  <link rel="stylesheet" type="text/css" href="css/style.css" />
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
  <script language="javascript" type="text/javascript" src="js/overlay.js"></script>
</head>
<body id="main">
  <p>
      <span id="test">0</span> match(es) found.
  </p>
</body>
</html>

我知道 jQuery 部分正在工作,因为我已经使用 jQuery DOM 选择进行了测试并且它可以工作(测试 id 按预期被修改)。基本上,如果一切正常,应该发生的只是一个显示"功能"的警报。但是,我总是收到一个错误,说:

Failed to perform AJAX call! textStatus: (parsererror) and errorThrown: (SyntaxError: Unexpected token <)

我认为这个错误与 godaddy 对 json 做了一些奇怪的事情有关,但我可能是错的。当我注释掉数据类型:"json"行时,警报确实显示功能正常。如果这真的是问题所在,我不确定"<"来自哪里?

叠加层.js

$(function() {
  $('#test').text( "text yes?" );
  $.ajax({
    url: 'overlay.php',
    data: "",
    dataType: 'json',
    success: function(data, textStatus, jqXHR) {
        alert("FUNCTIONAL");
    },
    error: function(jqXHR, textStatus, errorThrown){
        alert("Failed to perform AJAX call! textStatus: (" + textStatus +
              ") and errorThrown: (" + errorThrown + ")");
    }
  });
});

根据Chrome Developer Tools的说法,ajax调用运行覆盖.php没有任何错误。我知道我拥有的数据库连接可以工作,因为它已经在其他地方进行了测试。我知道我的查询工作,因为它已经在 SSH 的服务器上进行了测试。我认为这个文件中可能没有任何错误,但同样,我愿意接受建议,因为这让我发疯了。

叠加层.php

  $mysqli = mysqli_connect($hostname, $username, $password, $database);
  if(mysqli_connect_errno($mysqli)) {
    printf("Connect failed: %s'n", mysqli_connect_error());
    exit();
  }
  $qry = "
    SELECT paint_id, paint_name, paint_date, paint_media, paint_dim, paint_file 
    FROM paintings
    WHERE paint_id = '1'
  ";
  $results = mysqli_query($mysqli, $qry);
  $row = mysqli_fetch_assoc($results);    
  echo json_encode($row);
?>

完整代码的 URL 为:http://www.carlthomasedwards.com/painting2.php

我已经在 godaddy 上的 php.ini 文件中启用了 json。我也联系了他们寻求支持,这家伙完全没有头绪。还需要做一些特别的事情来让 ajax 调用在 godaddy 上工作吗?为什么错误消息显示"<"符号显示在 json 中?任何帮助不胜感激!

在 PHP 脚本中,您将数据库连接作为mysqli资源打开,但尝试将其作为mysql资源进行查询。不能混合使用两组调用。使用 mysqli 并删除mysql,因为后者已弃用。

以这种方式混合调用可能会导致以后的mysql代码失败,并给出您未检查或处理的错误消息。

如果错误消息是原始返回的,而不是预期的JSON结果,您将从Javascript获得您所看到的错误消息。

[编辑]

我看了一下你的网站。这是您的 ajax 调用返回的内容:

<br />
<b>Warning</b>:  mysql_query() [<a href='function.mysql-query'>function.mysql-query</a>]: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) in <b>/home/content/78/11658278/html/overlay.php</b> on line <b>29</b><br />
<br />
<b>Warning</b>:  mysql_query() [<a href='function.mysql-query'>function.mysql-query</a>]: A link to the server could not be established in <b>/home/content/78/11658278/html/overlay.php</b> on line <b>29</b><br />
<br />
<b>Warning</b>:  mysql_fetch_row() expects parameter 1 to be resource, boolean given in <b>/home/content/78/11658278/html/overlay.php</b> on line <b>30</b><br />
null