最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Failed to execute 'send' on 'XMLHttpRequest' - Stack Overflow

matteradmin11PV0评论

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 :

{"readyState":0,"status":0,"statusText":"NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load ''."}

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

<access origin="*" />
<access uri="*" subdomains="true" />
<feature name=".0/network"/>

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

And oh, here is my ajax call:

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.

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 :

{"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 ing. I added required lines to the res/xml/config.xml

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

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

And oh, here is my ajax call:

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.

Share Improve this question asked Feb 11, 2015 at 11:29 Kyu_Kyu_ 8004 gold badges10 silver badges19 bronze badges 4
  • jQuery.support.cors = true; - why are you overriding jQuery's cors detection? If a browser doesn't support cors, then telling jQuery it does won't help with anything. – Quentin Commented Feb 12, 2015 at 17:09
  • 1 Access-Control-Allow-* are response headers, sent by the server. Sending these headers in your request may be what's ruining your request. I have limited experience with Cordova's cross-origin behavior, though, so I don't know for sure. – apsillers Commented Feb 12, 2015 at 17:10
  • I added Jquery.support.cors and the 3 lasts setRequestHeader when I had my problem. It wasn't working before. – Kyu_ Commented Feb 13, 2015 at 11:57
  • Does it work without the async option set to false? – Jack Vial Commented Sep 28, 2015 at 14:49
Add a ment  | 

1 Answer 1

Reset to default -1

The jQuery documentation states:

Returning false in the beforeSend function will cancel the request

Since your beforeSend function doesn't have a return statement it will return undefined which is falsy. I assume that cancels your request (even though I didn't test it). Try returning true at the end of your beforeSend function.

Post a comment

comment list (0)

  1. No comments so far