简体   繁体   English

使用ajax将数据发布到本地服务器,无法设置请求名称

[英]Using ajax to post data to local server, cant set request name

I've created an AJAX request to send to my PHP server: 我已经创建了一个发送到我的PHP服务器的AJAX请求:

var xml = new XMLHttpRequest();
xml.open("POST", "question.php", true);
xml.setRequestHeader('question','test');
xml.send();

In PHP, I'm trying to receive the AJAX data, but unfortunetly, it's not working. 在PHP中,我正在尝试接收AJAX数据,但不幸的是,它无法正常工作。

$qst = $_POST['question'];
return $qst;

Am I doing something wrong with the AJAX request? 我是否在使用AJAX请求时出错? How do I set the name of the POST variable correctly? 如何正确设置POST变量的名称? I'm guessing that's the problem. 我猜这就是问题所在。

You have three problems that prevent your code from working. 您有三个问题阻止您的代码工作。

You are putting the data in the wrong place 您将数据放在错误的位置

POST data is sent in the body of the HTTP request, not the headers. POST数据在HTTP请求的正文中发送,而不是标头。

This is most easily achieved with the FormData object . 使用FormData对象最容易实现。 (You can also set the Content-Type request header and format the data manually, but FormData saves you from having to think about all of that). (您还可以手动设置Content-Type请求标头和格式化数据,但FormData使您不必考虑所有这些)。

 var data = new FormData();
 data.append("question", "test");

 var xml = new XMLHttpRequest();
 xml.open("POST", "question.php", true);
 xml.send(data);

You aren't looking at the response 你没有看到回应

Well. 好。 You might be looking at the response in the Network tab of the Developer Tools in your browser, but I'm guessing you are not. 您可能正在浏览器中的Developer Tools的Network选项卡中查看响应,但我猜您不是。

 xml.addEventListener("load", examine_response);

 function examine_response() {
     alert(this.responseText);
 }

Your PHP isn't outputting anything 你的PHP没有输出任何东西

return is used to pass data from a function back to whatever called it. return用于将函数中的数据传递回任何调用它的函数。

It won't output anything back to the browser. 它不会将任何内容输出回浏览器。 You need echo or similar for that. 你需要echo或类似的东西。

$qst = $_POST['question'];
print $qst;

Bonus problem: Security vulnerability! 奖金问题:安全漏洞!

Your code is vulnerable to XSS attacks. 您的代码容易受到XSS攻击。 You must not echo user input back in an HTML document without escaping it. 您不得在HTML文档中回显用户输入而不转义它。

Since you are trying to send plain text back, tell the browser it is a plain text document (PHP defaults to saying it is HTML): 由于您尝试发送纯文本,请告诉浏览器它是纯文本文档(PHP默认为它是HTML):

header("Content-Type: text/plain");
$qst = $_POST['question'];
print $qst;

使用$ _POST数组,您不会访问http标头,因为您只是在XMLHttpRequest中设置标头,它将无法工作,您需要在XMLHttpRequest中设置http主体。

You need to send the data through the AJAX send function (HTTP body), not by setting the HTTP headers. 您需要通过AJAX send功能(HTTP正文)发送数据,而不是通过设置HTTP标头。 Though, you do need to set the request header to show PHP that we're sending form data, which I've done below. 但是,您确实需要设置请求标头以显示我们正在发送表单数据的PHP,我在下面已经完成了。

var xml = new XMLHttpRequest();
xml.open("POST", "question.php", true);
xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xml.send("question=test");

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

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