我使用AJAX接收表单数据并使用POST方法将其发送到Web服务。我尝试使用Occurs check: cannot construct the infinite type: a ~ [a]
Expected type: [a] -> [a] -> [a]
Actual type: a -> [a] -> [a]
Relevant bindings include it :: [a] (bound at <interactive>:253:1)
In the first argument of ‘foldl’, namely ‘(:)’
In the expression: foldl (:) [] [1 .. 5]
检索信息,因为它将更改数据库中的数据。当ajax方法设置为$a = $_POST["accommodation"]
或POST
且Web服务正在使用时,它可以正常工作
GET
但不是$a = $_GET["accommodation"]
。
JS:
$a = $_POST["accommodation"]
PHP
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();
}
我尝试过以下操作,但仍然无法正常工作?
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"));
}
我也不能使用jquery。
答案 0 :(得分:1)
这里的问题是PHP的$_POST
和$_GET
与HTTP方法POST和GET没什么关系。
$_POST
包含来自请求正文的数据。 $_GET
包含来自URL的查询字符串的数据。
您正在对网址的查询字符串中的数据进行编码,因此它会显示在$_GET
中。
你需要把它放在身体里。您应该设置正确的Content-Type请求标头(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);
答案 1 :(得分:0)
这种情况正在发生,因为您没有将数据放入请求正文中。您将其附加到网址。 你应该使用这样的帖子:
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);
取自这里: Send POST data using XMLHttpRequest
我不知道这对于这个很酷的错误可以1+ 发送一些单独的控制数据可能很有用
答案 2 :(得分:-1)
我个人也不喜欢使用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。
答案 3 :(得分:-2)
为什么不使用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);
}
});