我如何通过与XMLHTT prequest变量变量、XMLHTT、prequest

2023-09-10 13:47:35 作者:都杀青了你还在演

我如何变量发送到服务器与 XMLHTT prequest ?请问我只是将它们添加到 GET 请求的URL的末尾,如?变量1 =?变量2 = ,等?

所以或多或少:

XMLHtt prequest(GET,blahblah.psp?变量1 =?+ VAR1 +?变量2 =+ VAR2,真)

解决方案

如果你想传递变量使用GET服务器,这将是这样没错。记住逃跑(urlen code)他们正确!

也可以使用POST,如果你不想你的变量是可见的。

http响应流程和xml解析 request,response

一个完整的示例是:

  VAR URL =bla.php;
VAR PARAMS =somevariable = someValue中和放大器; anothervariable = anothervalue;
VAR HTTP =新XMLHtt prequest();

http.open(GET,URL + +参数,真正的?);
http.onreadystatechange =功能()
{
    如果(http.readyState == 4和&安培; http.status == 200){
        警报(http.responseText);
    }
}
http.send(空);
 

要测试此,(使用PHP),你可以的var_dump $ _GET看你检索什么。

How do I send variables to the server with XMLHTTPRequest? Would I just add them to the end of the URL of the GET request, like ?variable1=?variable2=, etc?

So more or less:

XMLHttpRequest("GET", "blahblah.psp?variable1=?" + var1 + "?variable2=" + var2, true)

解决方案

If you want to pass variables to the server using GET that would be the way yes. Remember to escape (urlencode) them properly!

It is also possible to use POST, if you dont want your variables to be visible.

A complete sample would be:

var url = "bla.php";
var params = "somevariable=somevalue&anothervariable=anothervalue";
var http = new XMLHttpRequest();

http.open("GET", url+"?"+params, true);
http.onreadystatechange = function()
{
    if(http.readyState == 4 && http.status == 200) {
        alert(http.responseText);
    }
}
http.send(null);

To test this, (using PHP) you could var_dump $_GET to see what you retrieve.