简体   繁体   English

如何测试以查看是否通过Java正确发送了POST请求?

[英]How do I test to see if I am sending a POST request correctly via Javascript?

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. 这是一种特殊情况:我正在使用小书签来调用.js,以将POST请求发送到服务器上的PHP文件。 Here's the POST request in the .js file: 这是.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);

And below is what I'm doing in PHP. 下面是我在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). 但是,请尽我所能,我无法弄清楚如何将某些内容返回到.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";
?>

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... 最终,我想将这些变量并写入到MySQL数据库中,但是距离学习该方法至少还有一天的时间...

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. 在此过程中提供的任何帮助都将非常受欢迎,我花了很长时间找到有关处理用户表单未发送的POST请求的任何信息。 Apparently writing bookmarklets that send data to MySQL is a black art ;) 显然,编写将数据发送到MySQL的小书签是一门妖术;)

使用firebug的Firefox。

To test that you're doing it correctly, I'd probably use Firebug on Firefox or Dev Tools on Chrome; 为了测试您的操作是否正确,我可能会在Firefox上使用Firebug或在Chrome上使用开发工具; with either, you can see the actual HTTP data sent or received. 使用任一方法,您都可以查看发送或接收的实际HTTP数据。 But I think your real question is, why isn't the POST working? 但是我认为您真正的问题是,为什么POST不起作用? (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 . POST是通用的,您可以发布任何内容 In your case, you're posting URL-encoded data, so try adding: 对于您的情况,您要发布URL编码的数据,因此请尝试添加:

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

...after your open call. ...您的后open的呼叫。 Some examples here and here . 这里这里的一些例子。

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

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