简体   繁体   English

如何使用GET方法通过XMLHttpRequest传递FormData

[英]How to pass FormData over XMLHttpRequest using GET method

When Method of the senderform is POST, everything works fine. senderform为POST时,一切正常。 However, as soon as I change the method to GET, I don't receive anything on the server. 但是,一旦我将方法更改为GET,服务器上就什么也收不到。

function ajaxSubmit(destinationElement, senderform) {

    var xmlreq = new XMLHttpRequest();
    var params = new FormData(senderform);
    xmlreq.open(senderform.method, senderform.action, true);

    if (/\/content\.php$/.test(senderform.action))
        xmlreq.onreadystatechange = receiveTable;
    else xmlreq.onreadystatechange = receiveText;

    xmlreq.send(params);
}

I know that I could manually append key-value pairs at the end of Action address, but the problem is that I don't know which form is going to be passed with what fields. 我知道我可以在Action地址的末尾手动附加键值对 ,但是问题是我不知道哪种字段将传递哪种形式。

I would prefer native javaScript if possible. 如果可能,我希望使用本机javaScript。

How can I send a GET request using XMLHttpRequest with key-value pairs from senderform which points to form Element (the same way as it already works for POST requests)? 如何使用XMLHttpRequest和来自senderform的键-值对发送GET请求,该对值指向表单元素(与POST请求中已经使用的方式相同)?

In the end I came with this solution, which loads all key-value pairs from senderform form reference. 最后,我提供了此解决方案,该解决方案从senderform表单引用中加载所有键值对。

function ajaxSubmit(destinationElement, senderform) {
var xmlreq = new XMLHttpRequest();
var params = new FormData(senderform);

if (senderform.method == 'get')
{
    var firstRun = true;
    for (var key of params.keys())
    {
        if (firstRun)
        {
            senderform.action += '?';
            firstRun = false;
        }
        else senderform.action += '&';
      senderform.action += key + "=" + params.get(key);
    }
}
xmlreq.open(senderform.method, senderform.action, true);
if (senderform.action.indexOf('content.php')!==-1)
    xmlreq.onreadystatechange = receiveTable;
else xmlreq.onreadystatechange = receiveText;
xmlreq.send(params);
}

In combination with @tanc's answer and this article I was able to solve my problem. 结合@tanc的答案和本文,我能够解决我的问题。

Are you sure the problem is not the PHP script? 您确定问题不是PHP脚本吗? I see no reference that https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#send() with FormData needs POST to work, but if the PHP script takes the info from $POST or something (My PHP is rusty), the behavior would be different. 我看不到带有FormData的https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest#send()需要POST才能工作的引用,但是如果PHP脚本从$ POST或其他方式获取信息(我的PHP生锈了),其行为将有所不同。

Since you can't create a useable body in a GET request (see below), then the other option is to use params in the url. 由于您无法在GET请求中创建可用的正文(请参见下文),因此另一个选择是在url中使用参数。

 function buildGetUrlParams(baseUrl, paramsObj) { var builtUrl = baseUrl + "?"; Object.keys(paramsObj).forEach(function(key) { builtUrl += key + "=" + paramsObj[key] + "&"; }); return builtUrl.substr(0, builtUrl.length - 1); } document.getElementById('finalUrl').innerText = buildGetUrlParams('http://test.url.com', { name:'James', occupation:'web design' }); 
 <div id="finalUrl"></div> 

An HTTP GET request can contain a body, but there is no semantic meaning to that body. HTTP GET请求可以包含一个主体,但是该主体没有语义 Which means, in simple terms, that a server doesn't have any reason to, nor have any knowledge of how, to process the body of a GET request. 简单来说,这意味着服务器没有任何理由,也没有任何知识如何处理GET请求的主体。 If it's possible to write a server that could do this, it would be bad practice as per the HTTP/1.1 specs: 如果有可能编写可以执行此操作的服务器,那么按照HTTP / 1.1规范,这将是不正确的做法:

if the request method does not include defined semantics for an entity-body, then the message-body SHOULD be ignored when handling the request. 如果请求方法不包括为实体主体定义的语义,则在处理请求时应忽略消息主体。

And that's basically why it's not working. 这就是基本上不起作用的原因。 If you want to send any sort of data that the server is able to respond to, then you'll need to use a different HTTP method. 如果要发送服务器能够响应的任何类型的数据,则需要使用其他HTTP方法。

This answer also explains this issue. 这个答案也解释了这个问题。

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

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