gpt4 book ai didi

php - 使用ajax将数据发送到php时出现“不允许发布方法”错误

转载 作者:行者123 更新时间:2023-11-29 14:17:15 24 4
gpt4 key购买 nike

我想向 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 中工作,我需要将此网站放到网上才能正常工作吗?

最佳答案

查看提交按钮和表单 ID。 Razor 是否将其转换为有效的字符串?在我看来,id 似乎包含 @。根据 http://www.w3.org/TR/html4/types.html,id 值中不允许使用 @ 。这可能作为包含这些字符的值传递

    ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number
of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

尝试

@(row.id)

引用 - MVC3 Razor syntax. How do I convert this ASP.NET style to Razor. (also issues with @ within quotes)

关于php - 使用ajax将数据发送到php时出现“不允许发布方法”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12424978/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com