我如何通过与XMLHTT prequest变量 [英] How do I pass along variables with XMLHTTPRequest

查看:147
本文介绍了我如何通过与XMLHTT prequest变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

所以或多或少:

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

解决方案

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

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

一个完整的示例是:

  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.

这篇关于我如何通过与XMLHTT prequest变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆