引用错误:在尝试使用ajax发送数据时未定义

Reference error: is not defined, when trying to send data with ajax

本文关键字:ajax 数据 未定义 错误 引用      更新时间:2023-09-26

因此,我试图将数组作为字符串发送到.php文件,以将其写入数据库,但我收到了以下错误ReferenceError: strings is not defined

真的不知道发生了什么,第一次这么做。我从一个教程中复制了代码并开始修改它,它运行得很好,直到我在名称后面添加了字符串。这些词是保留的吗?我应该用什么来表达?它是如何工作的?谢谢你的帮助。

var string = [1, 2, 3];
$('#write').click(function() {
  writeTable();
})
// handles the click event, sends the query
function writeTable() {
  $.ajax({
    url: 'write.php',
    type: 'get', //data type post/get
    data: {
      name: $('input#input').val(),
      strings: JSON.stringify(strings)
    },
    complete: function(response) {
      $('.output').html(response.responseText);
    },
    error: function() {
      $('.output').html('Bummer: there was an error!');
    }
  });
  return false;
}
.button {
  border: 1px solid black;
  width: 100px;
  display: inline-block;
}
.output {
  height: 400px;
  width: 400px;
  border: 1px solid black;
  overflow: auto;
}
<!doctype HTML>
<head>
  <meta charset="UTF-8">
  <link type="text/css" rel="stylesheet" href="index.css">
</head>
<body>
  <br>
  <form>File Name:
    <input type="text" id="input" name="input" placeholder="Type something">
  </form>
  <br>
  <div class="button" id="write">Write</div>
  <div class="button" id="search">Search</div>
  <br>
  <br>
  <div class="output"></div>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  <script src="index.js"></script>
</body>

您声明:var string = [1, 2, 3];

并放入strings: JSON.stringify(strings)

您只缺少一个s或需要删除一个:

要解决此问题,您可以更改:

strings: JSON.stringify(strings)

至(不含s的第二个"字符串"):

strings: JSON.stringify(string)