简体   繁体   中英

Jquery Ajax call gives uncatchable error

I've gotten stuck on an ajax call that gives an error I can not catch with the error function in the ajax call. This error is keeping me from proceeding with the program

First off here is the code.

    var submit = function(){
        var url = ""
        var data = ["9b4e749d5cb6e069dd49cab93a69","aufujishngidrg"]
        for(var int = 0; int < data.length; int++) {
            console.log(data[int])
            url = *insert url*
            $.ajax({
                type:'GET',
                url:encodeURI(url),
                dataType: "json",
                contentType:"application/json; charset=utf-8",
                error:function (xmlHttpRequest,textStatus, xhr, ajaxOptions, thrownError) {
                    if(xmlHttpRequest.readyState == 0 || xmlHttpRequest.status == 0){
                        console.log("xmlhttpRequest error")
                        return;
                    }
                    else{
                        console.log("error")
                    }
                },
                success:function(data){
                    console.log(data)
                },
            });
        }
        console.log("derp")
    }

So basicly what I want to do is iterate over an array of hashes. These hashes are send to a server with an http request. The server should then return an Id belonging to the hash.

This works when you send a valid hash. It will just return the Id that belongs to the hash, no problems. The Id is logged to the console.

When you send a invlaid hash the server will respond with a status 500 error. This error is caught in the error function. BUT it also returns

XMLHttpRequest cannot load *url* No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

And I can't seem to catch this error. I don't even know why it occurs. I am aware of the cross domain policy and that is being dealt with server side. I am assuming it works because otherwise the valid request should return the same error right?

Does anyone know a work around or fix for this problem? What am i doing wrong? How do I catch this error? Thanks in advance!

One other thing I noticed is that the script will fully run, so outside of the ajax call will also log "derp" to the console. Then after that, it logs the ajax call output and errors. Should it not be: first, all ajax output then proceeding with the code after the call?

Try this :-

var submit = function(){
        var url = ""
        var data = ["9b4e749d5cb6e069dd49cab93a69","aufujishngidrg"]
        for(var int = 0; int < data.length; int++) {
            console.log(data[int])
            url = *insert url*
            $.ajax({
                type:'GET',
                url:encodeURI(url),
                crossDomain: true,
                dataType: 'jsonp',
                contentType:"application/json; charset=utf-8",
                error:function (xmlHttpRequest,textStatus, xhr, ajaxOptions, thrownError) {
                    if(xmlHttpRequest.readyState == 0 || xmlHttpRequest.status == 0){
                        console.log("xmlhttpRequest error")
                        return;
                    }
                    else{
                        console.log("error")
                    }
                },
                success:function(data){
                    console.log(data)
                },
            });
        }
        console.log("derp")
    }

Here's my working code :-

<!doctype>
<html>
<head>
  <title>Script</title>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  <script>
   // var submit = function(){
        var url = ""
        var data = ["9b4e749d5cb6e069dd49cab93a69","aufujishngidrg"]
        for(var int = 0; int < data.length; int++) {
            console.log(data[int])
            url = "contact.php";
            $.ajax({
                type:'GET',
                url:encodeURI(url),
                crossDomain: true,
                dataType: 'jsonp',
                contentType:"application/json; charset=utf-8",
                error:function (xmlHttpRequest,textStatus, xhr, ajaxOptions, thrownError) {
                    if(xmlHttpRequest.readyState == 0 || xmlHttpRequest.status == 0){
                        console.log("xmlhttpRequest error")
                        return;
                    }
                    else{
                        console.log("error")
                    }
                },
                success:function(data){
                    console.log(data)
                },
            });
        }
        console.log("derp")
   // }
  </script>
</head>
<body>
</body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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