js-传输数据给后台-1.1

    使用XMLHTTPRequest能够将数据传给后台。但这其中容易出现的问题你懂么?

POST

var xhr = new XMLHttpRequest();
xhr.open("POST", "", true);
xhr.send(JSON.stringify(_data));

    当我们的_data是一个对象时,传输中我们就会发现我们传输的是个request payload,Request Payload。

    那如果我们需要一个formdata,肿么办呢?

var _form = new FormData();
_form.append("value","A");

var xhr = new XMLHttpRequest();
xhr.open("POST", "", true);
xhr.send(_form);

GET

var xhr = new XMLHttpRequest();
xhr.open("POST", "/url?key=value&key1=value1", true);
xhr.send(null);

    我明明记得是可以通过send去发送数据。但我每次send数据,查看网络的时候,都会发现并没有发送。于是百度了许久,最终我还是妥协了。只能通过拼url的方式传数据了。各位如果知道怎么通过send发送数据,麻烦留言告诉我。

猜你喜欢

转载自blog.csdn.net/caishu1995/article/details/94725255