javascript 无法对“XMLHttpRequest”执行“发送”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28453213/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 09:00:32  来源:igfitidea点击:

Failed to execute 'send' on 'XMLHttpRequest'

javascriptajaxcordova

提问by Kyu_

I'm working on a Cordova application which is using ajax. My problem is that in debug, the app is working. But when I build a release, I got the error :

我正在开发一个使用 ajax 的 Cordova 应用程序。我的问题是在调试中,应用程序正在运行。但是当我构建一个版本时,我收到了错误:

{"readyState":0,"status":0,"statusText":"NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'https://sub.domain.tld'."}

I don't understand from where is it coming. I added required lines to the res/xml/config.xml

我不明白它从哪里来。我将所需的行添加到 res/xml/config.xml

<access origin="*" />
<access uri="*" subdomains="true" />
<feature name="http://api.phonegap.com/1.0/network"/>

And the permission INTERNET in the AndroidManifest. But the error is still happening...

以及AndroidManifest 中的INTERNET 权限。但错误仍在发生......

And oh, here is my ajax call:

哦,这是我的 ajax 调用:

jQuery.support.cors = true;
        $.ajax(
            {
                accepts: {
                    xml: "text/xml; charset=ISO-8859-1",
                    text: "text/plain; charset=ISO-8859-1"
                }
                , converters: {
                "text xml": jQuery.parseXML
            }
                , type: "POST"
                //, contentType: "text/xml; charset=ISO-8859-1"
                , contentType : "application/x-www-form-urlencoded; charset=iso-8859-1"
                , url: address
                , async: false
                , cache: false
                , crossDomain: false
                , data: msg
                , dataType: "text"
                , global: false
                , ifModified: false
                , username: this.params.server.login
                , password: this.params.server.pass
                , timeout: 500000
                , beforeSend: function(jqXHR) {
                    jqXHR.overrideMimeType('text/xml;charset=iso-8859-1');

                    jqXHR.setRequestHeader("Access-Control-Allow-Origin", "*");
                    jqXHR.setRequestHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
                    jqXHR.setRequestHeader("Access-Control-Allow-Methods","POST, GET, OPTIONS, DELETE, PUT, HEAD");
                }
            }
        )

Hope you'll be able to help me find a solution.

希望你能帮我找到解决办法。

回答by jonas

The jQuery documentationstates:

jQuery文档状态:

Returning false in the beforeSend function will cancel the request

在 beforeSend 函数中返回 false 将取消请求

Since your beforeSendfunction doesn't have a return statement it will return undefinedwhich is falsy. I assume that cancels your request (even though I didn't test it). Try returning trueat the end of your beforeSendfunction.

由于您的beforeSend函数没有 return 语句,它将返回undefinedfalse。我认为这会取消您的请求(即使我没有对其进行测试)。尝试truebeforeSend函数结束时返回。