简体   繁体   中英

Ajax PUT request successful but not updating on database

I am performing an AJAX PUT request which is coming back as successful but not updating on the database.

Have checked to make sure the variables going into the array are correct before being converted to JSON.

var recycle = {
    id:document.getElementById('id').value,
    year:document.getElementById('year').value,
    materialThousandTonnes:document.getElementById('material').value,
    packagingWasteArising:document.getElementById('packaging').value,
    totalRecoveredRecycled:document.getElementById('total').value,
    achievedRecoveryRecyclingRate:document.getElementById('achieved').value,
    euTargetRecoveryRecyclingRate:document.getElementById('target').value
};

$.ajax({
    url: '/recycle',
    method: 'PUT',
    data: JSON.stringify(recycle),
    contentType: "application/json;charset=utf-8",
    success: function (data) {
        alert("hi");
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert(jqXHR + '\n' + textStatus + '\n' + errorThrown);
    }
});

The alert "hi" appears everytime however it doesn't actually PUT the data into my database please help!

EDIT:

Here is server-side code for PUT request

 public function performPut($url, $parameters, $requestBody, $accept) 
    {
        global $dbserver, $dbusername, $dbpassword, $dbdatabase;

        $newRecycle = $this->extractRecycleFromJSON($requestBody);
        $connection = new mysqli($dbserver, $dbusername, $dbpassword, $dbdatabase);
        if (!$connection->connect_error)
        {
            $sql = "update recycledb set year = ?, materialThousandTonnes = ?, packagingWasteArising = ?, totalRecoveredRecycled = ?, achievedRecoveryRecyclingRate = ?, euTargetRecoveryRecyclingRate = ? where id = ?";
            // We pull the fields of the book into local variables since 
            // the parameters to bind_param are passed by reference.
            $statement = $connection->prepare($sql);
            $id = $newRecycle->getId();
            $year = $newRecycle->getYear();
            $materialThousandTonnes = $newRecycle->getMaterialThousandTonnes();
            $packagingWasteArising = $newRecycle->getPackagingWasteArising();
            $totalRecoveredRecycled = $newRecycle->getTotalRecoveredRecycled();
            $achievedRecoveryRecyclingRate = $newRecycle->getAchievedRecoveryRecyclingRate();
            $euTargetRecoveryRecyclingRate = $newRecycle->getEuTargetRecoveryRecyclingRate();
            $statement->bind_param('iisiiss', $id, $year, $materialThousandTonnes, $packagingWasteArising, $totalRecoveredRecycled, $achievedRecoveryRecyclingRate, $euTargetRecoveryRecyclingRate);
            $result = $statement->execute();
            if ($result == FALSE)
            {
                $errorMessage = $statement->error;
            }
            $statement->close();
            $connection->close();
            if ($result == TRUE)
            {
                // We need to return the status as 204 (no content) rather than 200 (OK) since
                // we are not returning any data
                $this->noContentResponse();
            }
            else
            {
                $this->errorResponse($errorMessage);
            }
        }
    }

So I found the solution to my issue after much a struggle

On this line:

$statement->bind_param('iisiiss', $id, $year, $materialThousandTonnes, $packagingWasteArising, $totalRecoveredRecycled, $achievedRecoveryRecyclingRate, $euTargetRecoveryRecyclingRate);

The id variable needed to be moved to the end of the statement...

Like this:

$statement->bind_param('iisiiss', $year, $materialThousandTonnes, $packagingWasteArising, $totalRecoveredRecycled, $achievedRecoveryRecyclingRate, $euTargetRecoveryRecyclingRate, $id);
$statement->bind_param('iisiiss', $year, $materialThousandTonnes, $packagingWasteArising, $totalRecoveredRecycled, $achievedRecoveryRecyclingRate, $euTargetRecoveryRecyclingRate, $id);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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