简体   繁体   English

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

[英]'post method not allowed' error while sending data to php with ajax

I want to send some data to php to change something in my MySQL database, but I keep getting this 405 error 'POST method not allowed'. 我想向PHP发送一些数据以更改MySQL数据库中的某些内容,但我不断收到此405错误“不允许使用POST方法”。 I googled a lot, but can't find any useful solution. 我在Google上搜索了很多,但是找不到任何有用的解决方案。 Can someone explain to me what I'm doing wrong? 有人可以向我解释我在做什么错吗?

I'm using Razor inside HTML to render the data on the webpage: 我在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>              
     }

then I have this JavaScript file: 然后我有这个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;
});

}); });

and my PHP looks like this: 和我的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());
 ?>

EDIT: 编辑:

Below is the link to a screenshot of the error info, does it has something to do with the static context? 下面是错误信息屏幕截图的链接,它与静态上下文有关吗? http://img41.imageshack.us/img41/9139/errorde.jpg http://img41.imageshack.us/img41/9139/errorde.jpg

I am working in Webmatrix using localhost, do I need to put this website online for it to work? 我正在使用localhost在Webmatrix中工作,是否需要将该网站置于联机状态才能正常工作?

Look at the submit button and form id. 查看提交按钮和表单ID。 Does the razor translate that into a valid string? 剃刀将其转换为有效的字符串吗? It looks to me as if the ids contain @ . 在我看来,id包含@ The @ in the id values is not permitted according to http://www.w3.org/TR/html4/types.html . 根据http://www.w3.org/TR/html4/types.html,id值中的@不允许使用。 This might be passed as values containing those characters 这可能作为包含这些字符的值传递

    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 (".").

Try 尝试

@(row.id)

Reference - MVC3 Razor syntax. 参考-MVC3 Razor语法。 How do I convert this ASP.NET style to Razor. 如何将这种ASP.NET样式转换为Razor。 (also issues with @ within quotes) (在引号内也有@问题)

Do you have firebug installed..please inspect the request over " what exactly happening to Ajax request" there and add exact error details here. 您是否安装了Firebug。请检查那里的“ Ajax请求到底发生了什么”请求,并在此处添加确切的错误详细信息。 SO I can help you. 因此,我可以为您提供帮助。

您必须在您的网络服务器的配置中允许POST方法

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

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