简体   繁体   English

使用 AJAX 将数据从 PHP 传递到 JavaScript 时遇到问题

[英]Having trouble with using AJAX to pass data from PHP to JavaScript

Ok, so, first of all, I apologize for the question, but I am fairly new to web development.好的,首先,我为这个问题道歉,但我对 Web 开发还很陌生。

I am trying to make a system in which the user can manipulate a database of events.我正在尝试制作一个用户可以操作事件数据库的系统。 I made it correctly with just php, but right now, I am supposed to add RESTful architecture to what I've already did, so I am trying to make REST API.我只用 php 正确地完成了它,但是现在,我应该将 RESTful 架构添加到我已经做过的事情中,所以我正在尝试制作 REST API。

I am trying to make an example of it: example.html has a form with the name of an event that will be added to the database when submitted to addEventToDB.php , which is supposed to then return to the html page, but has created an object with whether the operation was successful and the name that was updated.我正在尝试举一个例子: example.html有一个带有事件名称的表单,当提交到addEventToDB.php ,该表单将被添加到数据库中,然后应该返回到 html 页面,但是已经创建一个对象,包含操作是否成功以及更新的名称。 The midPoint.js script should be able to get that object and based on that, create on the html page, a paragraph stating the success of the operation and name of the event. midPoint.js脚本应该能够获取该对象,并基于该对象在 html 页面上创建一段说明操作成功和事件名称的段落。 For some reason, this never works and only returns the error Uncaught SyntaxError: Unexpected token < in JSON at position 0 , instead of adding said paragraph.出于某种原因,这永远不会奏效,只会返回错误Uncaught SyntaxError: Unexpected token < in JSON at position 0 ,而不是添加所述段落。

This is my first experiment, but I think it's the best way to add REST architecture to the project.这是我的第一个实验,但我认为这是将 REST 架构添加到项目中的最佳方式。

example.html示例.html

    <form action="addEventToDB.php" method="post" class="form-group list-group col-6 offset-3">
      <h1 class="display-3"> Add Event </h1>

      <div class="form-group">
        <label for="eventName">Name</label>
        <input type="text" name="eventName" id="eventName" class="form-control" placeholder="Name" aria-describedby="helpId">
      </div>

      <p id="demo">  </p>

      <button type="submit" class="btn btn-primary"> Add </button>
    </form>

midPoint.js中点.js

var xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function () {
    if (this.readyState == 4 && this.status == 200) {
        var responseArray = JSON.parse(this.responseText);

        document.getElementById("demo").innerHTML = responseArray.addResult;
    }
};

xmlhttp.open("GET", "addEventToDB.php", true);
xmlhttp.send();

addEventToDB.php addEventToDB.php

$eventName = $_POST['eventName'];

if(isset($eventName)){
    $sql = "INSERT INTO evento(`Nome`) VALUES ('{$eventName}')";
    $result = $conn->query($sql);

    if ($result === true) {
        $responseArray->addResult = true;
        $responseArray->uploadValue = $eventName;
    } else {
        $responseArray->addResult = false;
        $responseArray->uploadValue = $eventName;
    }
} else {
    $responseArray->addResult = false;
    $responseArray->uploadValue = $eventName;
}

$myJSON = json_encode($responseArray);

echo $myJSON;

header('Location: experiment.html');

I am trying to make sure that this project uses REST and this was the best way that I could think of adding it, but I can't seem to make it work.我试图确保这个项目使用 REST,这是我能想到的添加它的最佳方式,但我似乎无法让它工作。 That js file was supposed to get the data on the JSON file and print the paragraph, but I can't figure out how to make it work.该 js 文件应该获取 JSON 文件上的数据并打印该段落,但我不知道如何使其工作。

Your array must be like this to be used by json_encode你的数组必须像这样才能被 json_encode 使用

$responseArray= array(
   'item' => array(
     'addResult' => true,
     'uploadValue' => $eventName
   )
 );

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

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