我想向 php 发送一些数据以更改我的 MySQL 数据库中的某些内容,但我不断收到此 405 错误“不允许使用 POST 方法”。我用谷歌搜索了很多,但找不到任何有用的解决方案。有人可以向我解释我做错了什么吗?
我在 HTML 中使用 Razor 在网页上呈现数据:
@foreach(var row in db.Query(getKamers))
{
<form id="@row.id" action='_DataConn.php' method='post' class='ajaxform'>
<input type="text" value="@row.id" name="id" id="td-id" />
<input type="text" value="@row.oppervlakte" name="oppervlakte" id="td-opp" />
<input type="text" value="@row.locatie" name="locatie" id="td-loc" />
<input type="text" value="@row.type" name="type" id="td-type" />
<input type="text" value="@row.kamernr" name="nummer" id="td-kamernr" />
<input type="text" value="@row.vrij" name="vrij" id="td-vrij" />
<input type="submit" value="opslaan" name="opslaan" id="@row.id" />
</form>
}
然后我有这个 JavaScript 文件:
$(document).ready(function () {
$('.ajaxform').submit(function () {
$.ajax({
url: $(this).attr('action'),
type: $(this).attr('method'),
dataType: 'json',
data: $(this).serialize(),
success: function (data) {
console.log(data);
}
});
return false;
});
});
我的 PHP 看起来像这样:
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "root";
$dbname = "Studentenkamers";
//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
//Select Database
mysql_select_db($dbname) or die(mysql_error());
// Retrieve data from Query String
$id = $_POST['id'];
$oppervlakte = $_POST['oppervlakte'];
$locatie = $_POST['locatie'];
$kamernr = $_POST['nummer'];
$type = $_POST['type'];
$vrij = $_POST['vrij'];
echo 'ok'
// Escape User Input to help prevent SQL Injection
$id = mysql_real_escape_string($id);
$oppervlakte = mysql_real_escape_string($oppervlakte);
$locatie = mysql_real_escape_string($locatie);
$kamernr = mysql_real_escape_string($kamernr);
$type = mysql_real_escape_string($type);
$vrij = mysql_real_escape_string($vrij);
//build query
//"UPDATE Studentkamer SET oppervlakte='" + room[1] + "', locatie='" + room[2] + "', type='" + room[3] + "', vrij='" + room[4] + "' WHERE id='" + room[0] + "'";
$query = "UPDATE Studentkamer SET oppervlakte = '$id', locatie = '$locatie', type='$type', kamernr = '$kamernr', vrij = '$vrij' WHERE id='$id'";
//Execute query
$qry_result = mysql_query($query) or die(mysql_error());
?>
编辑:
下面是错误信息截图的链接,它与静态上下文有关吗? http://img41.imageshack.us/img41/9139/errorde.jpg
我正在使用 localhost 在 Webmatrix 中工作,我是否需要将此网站放到网上才能正常工作?