Javascript AJAX POST请求是否像GET一样?

Javascript AJAX POST请求是否像GET一样?,javascript,php,ajax,Javascript,Php,Ajax,我使用AJAX接收表单数据,并使用POST方法将其发送到web服务。我试图使用$a=$\u POST[“住宿”]检索信息,因为它将更改数据库中的数据。当ajax方法被设置为POST或GET并且web服务正在使用 $a=$\u获得[“住宿”]但不是$a=$\u发布[“住宿”] JS: function ajaxrequest() { var xhr2 = new XMLHttpRequest(); xhr2.addEventListener ("load", (e) => { va

我使用AJAX接收表单数据,并使用POST方法将其发送到web服务。我试图使用
$a=$\u POST[“住宿”]
检索信息,因为它将更改数据库中的数据。当ajax方法被设置为
POST
GET
并且web服务正在使用
$a=$\u获得[“住宿”]
但不是
$a=$\u发布[“住宿”]

JS:

function ajaxrequest()
{
var xhr2 = new XMLHttpRequest();

xhr2.addEventListener ("load", (e) =>
{
    var output = ""; // initialise output to blank text
    var data = JSON.parse(e.target.responseText);

    output = data;

    if(e.target.status==201)
{
    document.getElementById('response').innerHTML = "Successfuly booked!"
}
else if(e.target.status=404)
{
    document.getElementById('response').innerHTML = "Sorry fully booked for this date!"
}

});

var a = document.getElementById("accommodation").value;
var b = document.getElementById("username").value;
var c = document.getElementById("accid").value;
var d = document.getElementById("npeople").value;
var e = document.getElementById("date").value;


xhr2.open("POST" , "task2.php?accommodation=" + a + "&username=" + b + "&accid=" + c + "&npeople=" + d + "&date=" + e);

xhr2.send();

}
PHP

header("Content-type: application/json");

$a = $_POST["accommodation"];
$b = $_POST["npeople"];
$c = $_POST["date"];
$d = $_POST["username"];
$e = $_POST["accid"];

$conn = new PDO ("mysql:host=localhost;dbname=***;", "***", "***");

$result = $conn->query("select * from acc_dates where accid=$e and thedate=$c");
$row = $result->fetch();

if($row["availability"] >= $b)
{

    echo json_encode(header("HTTP/1.1 201 Created"));   

    $result = $conn->query("insert into acc_bookings (accID, thedate, username, npeople) values ($e, $c, '$d', $b)");
    $result = $conn->query("update acc_dates set availability = availability + -$b where accid=$e and thedate=$c");
}
else
{
    echo json_encode(header("HTTP/1.1 404 Not Found")); 

}

我试着做了以下操作,但仍然不起作用

  xhr2.open("POST" , "task2.php");

  xhr2.send("accommodation=" + a + "&username=" + b + "&accid=" + c + 
            "&npeople=" + d + "&date=" + e);

我也不能使用jquery。

为什么不直接使用jquery ajax功能。
你可以试试这个,我希望它对你有用

var a = $('#accommodation').val();
var b = $('#username').val();
var c = $('#accid').val();
var d = $('#npeople').val();
var e = $('#date').val();
$.ajax({
        url:'task2.php',
        type:'post',
        data:{accommodation:a,username:b,accid:c, npeople:d, date:e},
        dataType:'json',
        success:function(response){
            console.log(response);
        }
    });

我个人也不喜欢使用JQuery。因为本机Javascript能够处理一切甚至更多

对于你的情况,你应该写

xhr2.open("POST", "task2.php", true);
xhr2.setRequestHeader("Content-type","application/x-www-form-urlencoded"); // crucially important!
xhr2.send("accommodation=" + a + "&username=" + b + "&accid=" + c + 
        "&npeople=" + d + "&date=" + e);

这应该行得通,您可以进一步询问有关AJAX的问题。我为操作AJAX编写了自己的库。

这里的问题是PHP的
$\u POST
$\u GET
与HTTP方法POST和GET没有太大关系

$\u POST
包含来自请求正文的数据
$\u GET
包含URL查询字符串中的数据

您正在对URL的查询字符串中的数据进行编码,因此它将出现在
$\u GET

你需要把它放在身体里。您应该设置正确的内容类型请求头(
application/x-www-form-urlencoded
,这是表单提交使用的默认格式,也是您已经使用的格式),以便PHP知道如何解码您发送的数据。您还应该正确地转义特殊字符

xhr2.open("POST" , "task2.php");
xhr2.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
var body = "accommodation=" + encodeURIComponent(a) + 
           "&username=" + encodeURIComponent(b) + 
           "&accid=" + encodeURIComponent(c) + 
           "&npeople=" + encodeURIComponent(d) + 
           "&date=" + encodeURIComponent(e);
xhr2.send(body);

这是因为您没有将数据放入请求主体中。您正在将其附加到url。 您应该像这样使用post:

  var http = new XMLHttpRequest();
  var url = "get_data.php";
  var params = "lorem=ipsum&name=binny";
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", 
"application/x-www-form-urlencoded");

http.onreadystatechange = function() {//Call a function when the state changes.
    if(http.readyState == 4 && http.status == 200) 
{
        alert(http.responseText);
   }
}
 http.send(params);
摘自这里:

我不知道这会对这个很酷的错误起到1+的作用
发送一些单独的控制数据可能很有用,这是因为您实际上没有发送任何
POST
数据。URL中的键值对始终转换为
GET
参数;如果你想发送
POST
数据,你需要把它放在你的
send()
调用中:可能是他们所说的重复。你真的必须检查这个叫做prepared语句的东西。(你的php是不安全的)我试着做了下面的事情,但仍然不起作用?open(“POST”,“task2.php”);xhr2.send(“住宿=“+a+”&username=“+b+”&accid=“+c+”&npeople=“+d+”&date=“+e”);使用
$\u REQUEST['accountment']
它将解析
php
GET
POST
请求的值-OP没有使用jQuery,如果使用了,则会出现
$.POST()
text/plain
?这些数据不是纯文本,PHP不会决定它。OOP,我可能混淆了C++ CGI响应头。请原谅我,我有一段时间没有用PHPAJAX编写代码了。