【问题标题】:JSON.parse not running [closed]JSON.parse 未运行 [关闭]
【发布时间】:2023-03-24 19:14:01
【问题描述】:

我的 JSON.parse 代码行没有正常运行。有任何想法吗?在运行“showconsultationdata”函数时,应该显示 2 个警报框,但我的第二个警报框没有运行,因为程序的运行总是在 JSON.parse 行处停止。 这是我的 AJAX 脚本。

function showconsultationdata(str) { //face e.g and checkboxes for that date selected.
var xmlhttp;
if (str == "") {
    document.getElementById("txtHint2").innerHTML = "";
    return;
} else { 
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            alert("hi");
          document.getElementById("txtHint2").innerHTML = xmlhttp.responseText;             
          var a = JSON.parse(xmlhttp.responseText);
          document.getElementById("test").value=a.first;
          alert("bye");
        }
    }
    xmlhttp.open("GET","BAConsultRecordsAJAX.php?q="+str,true);
    xmlhttp.send();
}
}

这是我的 BAConsultRecordsAJAX.php 文件。

<?php
session_start();
include('Config/Js.php');
$q = $_GET['q'];
$consult="SELECT * FROM Counsel where nric='$_SESSION[nric]' and dateconsulted='$q'";
$consultresult = mysqli_query($dbconn,$consult);
while($row = mysqli_fetch_array($consultresult)) {
$queryResult[] = $row['skincareremarks'];
$queryResult[] = $row['skinconditionremarks'];
}
$skincareremarks = $queryResult[0];
$skinconditionremarks = $queryResult[1];
echo json_encode(array('first'=>$skincareremarks,'second'=>$skinconditionremarks));

哦,我尝试提醒 xmlhttp.responseText,这就是我得到的。 如您所见,在最后一行中,它正确显示了响应文本值。但我不确定为什么它显示我的 javascripts svc

【问题讨论】:

  • 任何控制台错误?
  • 定义正常运行。脚本返回什么字符串
  • 1) 利用 mysqli 的参数绑定。事实上,您对 SQL 注入攻击持开放态度,而 $_SESSION 变量可能会造成问题。 2)xmlhttp.responseText的值是多少?我敢打赌它不会像有效的那样回来。
  • 利用浏览器控制台。 F12
  • @yuriy636 我收到了这个错误。 VM95:2 Uncaught SyntaxError: Unexpected token

标签: javascript php jquery json ajax


【解决方案1】:

但我不确定它为什么显示我的 javascripts svc

include('Config/Js.php'); 因为你包含的脚本可能会回应它们?那些将 HTML 添加到响应中导致其变为无效 JSON 的人。

【讨论】:

  • 是的,我想也是这样。你知道我如何在一个 php 文件中放置多个 SRC,因为我的其他 javascript 函数使用其他 SRC。我已经尝试过
  • 在您的代码中,我没有看到使用 include('Config/Js.php'); 的理由,只需删除该行即可。
  • 如果我删除该行,我的 AJAX 和按钮将无法工作,因为 javascript 函数没有 SRC。我认为有一种方法可以从我想要的 responseText 中选择特定数据。但我不知道该怎么做。
【解决方案2】:

我设法通过过滤掉我的 xmlhttp.responseText 中显示的内容来解决问题。

在我的 BAConsultRecordsAJAX.php 文件中,我这样做了。

echo "<div id='test1'>";
echo json_encode(array('first'=>$skincareremarks,'second'=>$skinconditionremarks));
echo "</div>";

我给这个输出一个名为'test1'的div。

然后,在我的主文件的 AJAX 脚本中,我这样做了。

var a = JSON.parse($(xmlhttp.responseText).filter('#test1').html());    
document.getElementById("test").value=a.first;

所以基本上,它会过滤掉其余的 xhtmlhttp.responseText 输出,并只选择 div id='test1'。

希望这对那些也有这个问题的人有所帮助..

【讨论】:

    猜你喜欢
    • 2016-04-26
    • 2012-07-03
    • 2011-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    相关资源
    最近更新 更多