简体   繁体   English

XMLHttpRequest将数据发送到哪里?

[英]XMLHttpRequest where to place data to send?

I tried it like this but while the request was made it sent no data to the server. 我尝试了这样但是在请求发出时它没有向服务器发送数据。

 var data ={some:'data'};     
 var req = new XMLHttpRequest();
 req.open("GET", url, true);
 req.send(data);

The XHR has no built in method for converting an object to a querystring. XHR没有用于将对象转换为查询字符串的内置方法。 Additionally, the send() method takes an argument as the request body (which is for POST data). 此外, send()方法将参数作为请求主体(用于POST数据)。 GET requests have no request body, only the URL and headers GET请求没有请求正文,只有URL和标题

You could easily make a helper function to transform your object into a querystring like so: 您可以轻松地创建一个帮助函数来将对象转换为查询字符串,如下所示:

function convertToQuery(data){
    var first = true, q = '', amp = '';
    for(var key in data){
        amp = first ? '' : '&';
        q += amp + key + '=' + encodeURIComponent(data[key]);
        first = false;
    }

    return q;
}


var data ={some:'data'};
var url = 'page.html?' + convertToQuery(data);
var req = new XMLHttpRequest();
req.open("GET", url, true);
req.send(null);

You can either pass data as arguments in the GET method doing this: 你可以在GET方法中传递数据作为参数:

var data ={some:'data'};     
var req = new XMLHttpRequest();
req.open("GET", url + '?some=' + data.some, true);
req.send();

but notice that this is a weak solution since if you need to add more parameters in a near future you will need to edit this URL, also, the data is not properly escaped for the URL format (you should use the encodeURIComponent() function to properly escape every string). 但请注意,这是一个弱解决方案,因为如果您需要在不久的将来添加更多参数,您将需要编辑此URL,同样,数据未正确转义为URL格式(您应该使用encodeURIComponent()函数来正确地逃避每一根绳子)。

A simple code snippet addressing this would be something like this: 一个简单的代码片段就是这样的:

function getEscapedURL(url, data) {
    url += "?";
    for(key in data) {
        url += (key + '=' + encodeURIComponent(data[key]) + '&'); 
    }
    return url;
}

HEADS UP: if the data you are sending to the server is potentially considered to be sensitive (login, personal, financial, etc) you should NOT use this approach. 请注意:如果您要发送到服务器的数据有可能被认为是敏感(登录,个人,财务等),你应该使用这种方法。

Instead consider using the POST or PUT methods depending on the action. 而是根据操作考虑使用POST或PUT方法。 The GET method should be used to query for resources, data or information of the server (that's why is named the query string), while the POST and PUT should be used to submit sensitive information to the server. 应该使用GET方法来查询服务器的资源,数据或信息(这就是为什么命名为查询字符串),而POST和PUT应该用于向服务器提交敏感信息。

Being that pointed out, you might want to consider doing the next approach: 正如所指出的那样,您可能需要考虑采用下一种方法:

var data ={some:'data'};     
var req = new XMLHttpRequest();
req.open("POST", url, true);
req.send(JSON.stringify(data));

In this approach you serialize the data into a JSON string that will be received by the server, the server then should parse it and use that data. 在此方法中,您将数据序列化为将由服务器接收的JSON字符串,然后服务器应解析它并使用该数据。

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

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