简体   繁体   English

GoDaddy托管上带有dataType JSON解析的AJAX错误

[英]AJAX with dataType JSON parse error on GoDaddy hosting

EDIT: I have since switched everything over to mysqli. 编辑:从那以后,我已将所有内容切换到mysqli。 I had another script that did everything correctly in mysqli, but in my haste I copied a portion of someone else's without noticing it was done in mysql. 我有另一个脚本可以在mysqli中正确地执行所有操作,但是我匆忙地复制了一部分别人的脚本,却没有注意到它是在mysql中完成的。 Looks like this fixes that problem, thanks everyone! 看起来这个解决了这个问题,谢谢大家! I have edited my code below so that it can be used as a resource for anyone who's fighting a similar problem. 我在下面编辑了我的代码,以便可以将其用作解决类似问题的任何人的资源。

I am having some trouble getting ajax to work on a godaddy shared hosting account. 我在使Ajax在Godaddy共享主机帐户上工作遇到一些麻烦。 I've used it before on another sever without any trouble, so I'm thinking it's something odd about the godaddy server, though I'd be happy to be incorrect. 我以前曾在另一台服务器上使用过它而没有任何麻烦,所以我认为Godaddy服务器有点奇怪,尽管我很乐意弄错。 I tried writing up this code in perl first but turns out godaddy doesn't support JSON.pm, so I switched over to php since that seems to be godaddy's language of choice. 我试过先用perl编写此代码,但发现godaddy不支持JSON.pm,所以我切换到php,因为这似乎是godaddy的选择语言。 I'm still throwing an error at the ajax call. 我仍然在ajax调用中引发错误。

Here's a toy version of each part. 这是每个部分的玩具版本。 The only part of the HTML that's pertinent is the call to overlay.js and the test id secton to show that jQuery is working. 与HTML相关的唯一部分是对overlay.js的调用和测试ID secton,以显示jQuery在起作用。

The HTML below calls overlay.js: 下面的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>

I know that the jQuery part is working because I've tested using jQuery DOM selection and it works (the test id gets modified as expected). 我知道jQuery部分是有效的,因为我已经使用jQuery DOM选择进行了测试并且可以正常工作(测试ID已按预期进行了修改)。 Basically all that should happen if everything is working is an alert that says "FUNCTIONAL". 基本上,如果一切正常,所有应该发生的事情是显示“ FUNCTIONAL”的警报。 However, I always get an error that says: 但是,我总是收到一条错误消息:

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

I think that the error has to do with godaddy doing something oddly with json, but I could be wrong. 我认为该错误与Godaddy对json的处理有些奇怪,但我可能是错的。 When I comment out the dataType: 'json' line the alert does say functional. 当我注释掉dataType:'json'行时,警报确实说功能正常。 I'm not sure where a "<" would be coming from if that's actually the problem? 如果这确实是问题,我不确定“ <”从哪里来?

overlay.js overlay.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 + ")");
    }
  });
});

The ajax call, according to Chrome Developer Tools, runs overlay.php without any errors. 根据Chrome开发者工具的说法,ajax调用可以运行overlay.php而不会出现任何错误。 I know that the database connection I have works as it has been tested elsewhere. 我知道我拥有的数据库连接可以正常工作,因为它已经在其他地方进行了测试。 I know that my query works as it has been tested on the server in SSH. 我知道我的查询可以正常工作,因为它已经在SSH服务器上进行了测试。 I don't think there are likely any errors in this file, but again, I'm open to suggestion since this is driving me nuts. 我认为该文件中可能没有任何错误,但是再次,我很愿意提出建议,因为这使我发疯。

overlay.php overlay.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);
?>

The URL for the full code is: http://www.carlthomasedwards.com/painting2.php 完整代码的URL为: http : //www.carlthomasedwards.com/painting2.php

I have enabled json in my php.ini file on godaddy. 我在godaddy的php.ini文件中启用了json。 I also contacted them for support and the guy was completely clueless. 我还联系了他们寻求支持,这家伙完全一无所知。 Is there something else special that needs to be done to get ajax calls working on godaddy? 还有其他一些特别的事情需要做才能使agox调用在Godaddy上正常工作吗? Why is the error saying a "<" symbol is showing up in the json? 为什么在json中显示“ <”符号的错误提示? Any help is appreciated! 任何帮助表示赞赏!

In your PHP script you're opening the DB connection as a mysqli resource, but attempting to query it as a mysql resource. 在您的PHP脚本中,您正在将数据库连接作为mysqli资源打开,但尝试将其作为mysql资源进行查询。 You can't mix the two sets of calls. 您不能将两组呼叫混在一起。 Use mysqli and drop mysql since the latter is deprecated. 使用mysqli并删除mysql因为后者已被弃用。

Mixing the calls this way is likely to cause your later mysql code to fail and give an error message which you aren't checking for or handling. 以这种方式混合调用可能会导致您以后的mysql代码失败并给出一条错误消息,您没有检查或处理。

If the error message is being returned raw instead of the expected JSON result you'll get exactly the sort of error message from Javascript that you're seeing. 如果错误消息是原始返回的,而不是预期的JSON结果,则您将从所看到的Javascript中获得确切的错误消息。

[edit] [编辑]

I took a look at your site. 我看了你的网站。 This is what is being returned by your ajax call: 这是您的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM