Javascript 如何使用 XMLHTTPRequest 传递变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8064691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I pass along variables with XMLHTTPRequest
提问by Cecil Rodriguez
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?
如何将变量发送到服务器XMLHTTPRequest
?我会不会将它们添加到GET
请求的 URL 的末尾,例如?variable1=?variable2=
,等等?
So more or less:
所以或多或少:
XMLHttpRequest("GET", "blahblah.psp?variable1=?" + var1 + "?variable2=" + var2, true)
回答by TJHeuvel
If you want to pass variables to the server using GET that would be the way yes. Remember to escape (urlencode) them properly!
如果您想使用 GET 将变量传递给服务器,那将是肯定的。记住正确地转义(urlencode)它们!
It is also possible to use POST, if you dont want your variables to be visible.
如果您不希望变量可见,也可以使用 POST。
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.
要对此进行测试,(使用 PHP)您可以var_dump $_GET
查看检索到的内容。
回答by James Forbes
Manually formatting the query string is fine for simple situations. But it can become tedious when there are many parameters.
对于简单的情况,手动格式化查询字符串是很好的。但是当有很多参数时它会变得乏味。
You could write a simple utility function that handles building the query formatting for you.
您可以编写一个简单的实用程序函数来为您构建查询格式。
function formatParams( params ){
return "?" + Object
.keys(params)
.map(function(key){
return key+"="+encodeURIComponent(params[key])
})
.join("&")
}
And you would use it this way to build a request.
您可以通过这种方式使用它来构建请求。
var endpoint = "https://api.example.com/endpoint"
var params = {
a: 1,
b: 2,
c: 3
}
var url = endpoint + formatParams(params)
//=> "https://api.example.com/endpoint?a=1&b=2&c=3"
There are many utility functions available for manipulating URL's. If you have JQuery in your project you could give http://api.jquery.com/jquery.param/a try.
有许多实用函数可用于操作 URL。如果您的项目中有 JQuery,您可以尝试http://api.jquery.com/jquery.param/。
It is similar to the above example function, but handles recursively serializing nested objects and arrays.
它类似于上面的示例函数,但处理递归序列化嵌套对象和数组。
回答by mellamokb
The correct format for passing variables in a GET request is
在 GET 请求中传递变量的正确格式是
?variable1=value1&variable2=value2&variable3=value3...
^ ---notice &--- ^
But essentially, you have the right idea.
但本质上,您的想法是正确的。
回答by AuxTaco
If you're allergic to string concatenation and don't need IE compatibility, you can use URL
and URLSearchParams
:
如果您对字符串连接过敏并且不需要 IE 兼容性,您可以使用URL
and URLSearchParams
:
const target = new URL('https://example.com/endpoint');
const params = new URLSearchParams();
params.set('var1', 'foo');
params.set('var2', 'bar');
target.search = params.toString();
console.log(target);
Or to convert an entire object's worth of parameters:
或者转换整个对象的参数值:
const paramsObject = {
var1: 'foo',
var2: 'bar'
};
const target = new URL('https://example.com/endpoint');
target.search = new URLSearchParams(paramsObject).toString();
console.log(target);
回答by Muhammad Junaid Iqbal
Following is correct way:
以下是正确方法:
xmlhttp.open("GET","getuser.php?fname="+abc ,true);
回答by cowls
Yes that's the correct method to do it with a GET request.
是的,这是使用 GET 请求执行此操作的正确方法。
However, please remember that multiple query string parameters should be separated with &
但是,请记住,多个查询字符串参数应该用 & 分隔
eg. ?variable1=value1&variable2=value2
例如。?变量1=值1&变量2=值2
回答by ENDEESA
How about?
怎么样?
function callHttpService(url, params){
// Assume params contains key/value request params
let queryStrings = '';
for(let key in params){
queryStrings += `${key}=${params[key]}&`
}
const fullUrl = `${url}?queryStrings`
//make http request with fullUrl
}