如何测试我是否通过 Javascript 正确发送 POST 请求?

发布于 2024-09-07 05:13:42 字数 968 浏览 9 评论 0原文

这是特殊情况:我使用一个书签来调用一个 .js,该 .js 向我的服务器上的 PHP 文件发送 POST 请求。这是 .js 文件中的 POST 请求:

var snd = ("qu=" + encodeURIComponent(t) + "&dl=" + encodeURIComponent(dl) + "&dt=" + encodeURIComponent(dt));


xr = new XMLHttpRequest();   
xr.open("POST", "http://quotebook.us/s/process2.php",true);
xr.onreadystatechange=function() {
  if (xr.readyState==4) {
    var xmldoc = xr.responseText;
window.alert(xr.responseText);
}
}

xr.send(snd);

下面是我在 PHP 中所做的事情。但尽我所能,我无法弄清楚如何将某些内容返回到 .js 文件,以便它可以在警报中显示它(因此,我可以确认它首先发送了数据)。

<?php

if ($_SERVER['REQUEST_METHOD'] != 'POST') {
    echo "This page is not for viewing";
    exit;
} 
$qo = $_POST["qu"];
$dl = $_POST["dl"];
$dt = $_POST["dt"];

echo "First parm: $qo, second param: $dl, third param: $dt";
?>

最终,我想获取这些变量并将它们写入 MySQL 数据库,但我至少需要一天的时间才能学习如何做到这一点...

对此过程的任何帮助将非常受欢迎,我已经有很多了一次查找有关处理不是由用户表单发送的 POST 请求的任何内容。显然,编写将数据发送到 MySQL 的书签是一门黑术;)

Here's the particular situation: I'm using a bookmarklet to call a .js that sends a POST request to a PHP file on my server. Here's the POST request in the .js file:

var snd = ("qu=" + encodeURIComponent(t) + "&dl=" + encodeURIComponent(dl) + "&dt=" + encodeURIComponent(dt));


xr = new XMLHttpRequest();   
xr.open("POST", "http://quotebook.us/s/process2.php",true);
xr.onreadystatechange=function() {
  if (xr.readyState==4) {
    var xmldoc = xr.responseText;
window.alert(xr.responseText);
}
}

xr.send(snd);

And below is what I'm doing in PHP. But try as I might, I can't figure out how to get something BACK to the .js file so it can display it in an alert (and consequently, so I can confirm that it's sending the data in the first place).

<?php

if ($_SERVER['REQUEST_METHOD'] != 'POST') {
    echo "This page is not for viewing";
    exit;
} 
$qo = $_POST["qu"];
$dl = $_POST["dl"];
$dt = $_POST["dt"];

echo "First parm: $qo, second param: $dl, third param: $dt";
?>

Ultimately I want to take these variables and write them to a MySQL database, but I'm at least a day away from learning how to do that...

Any help on this process would be very welcome, I've had a heck of a time finding anything about processing POST requests that AREN'T sent by a user form. Apparently writing bookmarklets that send data to MySQL is a black art ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我最亲爱的 2024-09-14 05:13:42

对于 Firefox,请使用 firebug

Use firebug for firefox.

恋你朝朝暮暮 2024-09-14 05:13:42

为了测试您是否正确执行,我可能会在 Firefox 上使用 Firebug 或在 Chrome 上使用开发工具;无论使用哪种方式,您都可以看到发送或接收的实际 HTTP 数据。但我认为你真正的问题是,为什么 POST 不起作用? (您可以考虑更新问题标题。)

答案可能是您没有设置内容类型。 POST 是通用的,您可以发布任何内容。在您的情况下,您要发布 URL 编码的数据,因此请尝试

xr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

open 调用之后添加: ...。 此处此处

To test that you're doing it correctly, I'd probably use Firebug on Firefox or Dev Tools on Chrome; with either, you can see the actual HTTP data sent or received. But I think your real question is, why isn't the POST working? (You might consider updating your question title.)

And the answer may be that you're not setting the content type. POST is generic, you can post anything. In your case, you're posting URL-encoded data, so try adding:

xr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

...after your open call. Some examples here and here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文