简体   繁体   English

如何测试我的XMLHttpRequest是否工作?

[英]How to test if my XMLHttpRequest worked?

(sorry for the bad english) (对不起,英语不好)

I'm a beginner in js and php I would like to know how to test if my POST request worked, 我是js和php的初学者,我想知道如何测试POST请求是否有效,

The code which send the request : 发送请求的代码:

var nom2 = document.getElementById('nom2').value;
var data2 = document.getElementById('image_upload').value;
returnValue = "nom2="+nom2+"&data2="+data2;
var requete = createXmlHttpRequestObject();
requete.open('POST', "photo2.php", true);
requete.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
requete.send(returnValue);

And the code to test if my request worked 和测试我的请求是否有效的代码

if (isset($_POST['nom2']) && isset($_POST['data2']))
{
    echo"<script  type='text/javascript'>   message(); </script>";
}

The problem is that nothing is writed into the console log, and when i try to echo nothing is writed too. 问题是没有任何内容写入控制台日志,并且当我尝试回显任何内容时也没有写入。 Need some help please ! 请需要帮助!

Thanks ! 谢谢 !

You have to create a callback. 您必须创建一个回调。 This is a function that is called when the browser finished your request. 当浏览器完成您的请求时,将调用此函数。

var nom2 = document.getElementById('nom2').value;
var data2 = document.getElementById('image_upload').value;
var parameters = "nom2="+nom2+"&data2="+data2;

var http = new XMLHttpRequest();
http.open("POST", "photo2.php", 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) {
        console.log(http.responseText); // <= Here your browser prints the response that came from the php echo to the browser console
    }
}
http.send(parameters);

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

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